Table of contents
1.
Introduction
2.
Pointers
2.1.
Syntax
2.2.
Example 1
2.3.
Example 2
3.
Rules of Pointers in Carbon
4.
Advantages of Pointers
5.
Frequently Asked Questions
5.1.
Who created Carbon?
5.2.
What will Carbon language be used for?
5.3.
Is carbon language garbage collected?
5.4.
What is the difference between Carbon and C++?
5.5.
How can we compile Carbon on windows?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Pointers in Carbon

Author Sagar Mishra
0 upvote

Introduction

Hey Ninjas! Do you know Google launched a new programming language this year? Yes, and they call it Carbon. Carbon is a successor language of C++. Carbon is a language introduced in July 2022. You must have heard the name Pointers, which is common in most programming languages.

Today we will discuss the topic of Pointers in Carbon. Let's start without wasting time.

Pointers in Carbon

Pointers

Pointers

A pointer is a variable that stores the address of memory. And the value is the address of the other variable of the same data type. The * operator is used to dereference the variable's value that the pointer points to. 

Pointers Diagramatical Example

Here, in the left section (In the code), we assigned a value of 10 to the variable x. Then the x went to the memory address 12 and stored the value 10 to it. Now we again assign a pointer variable pointing to the address of x. Now it stores the address of x in the memory, as shown in the figure above.

Syntax

Syntax to declare any Pointers in Carbon is

var p: i32* = &*x; 


Here, 

  • p is the variable name.
     
  • i32* is the return type of variable p.
     
  • x is the value whose address will be stored in the variable p.

Example 1

Let's take a simple example that will clear all your doubts about Pointers in Carbon.

package NinjaTest api;

fn Main() -> i32 {

    var x: i32 = 5;

    x = 10;
   
    var p: i32* = &x;
    *p = 8;
   
    Print("Value of x: {0}", x);
    return 0;
}


Output:

Output of Example 1

Explanation 

Now we will discuss the example code. Let's start.

In the above code,

  • First, we entered the main function.
     
  • Then, we declared a variable x and assigned it a value of 5.
     
  • In the next step, we explicitly changed the value of x from 5 to 10.
     
  • Now we declared a variable p of type pointer and assigned it the address of x (&x).
     
  • Next, we changed the value of pointer p to 8.
     
  • And at the end, we printed the value of x.

Example 2

Let's take one more simple example that will clear all your doubts about Pointers in Carbon.

package NinjaTest api;

fn Main() -> i32 {

  var x: i32 = 8;
  x = 15;
  Print("---");
  Print("x = {0}", x);
  
  var y: i32* = &x;
  *y = 10;
  Print("---");
  Print("x = {0}", x);
  Print("y = {0}", *y);
  
  var z: i32* = &*y;
  *z = 5;
  Print("---");
  Print("x = {0}", x);
  Print("y = {0}", *y);
  Print("z = {0}", *z);

  return 0;
}


Output:

Output of Example 2

Explanation 

In the above code,

  • First, we entered the main function.
     
  • Then, we declared a variable x and assigned it a value of 8.
     
  • In the next step, we explicitly changed the value of x from 8 to 15.
     
  • Now we printed the value of x.
     
  • We declared a pointer variable y, and this time we assigned it the address of x.
     
  • Now, we set the value 10 to the pointer of y.
     
  • Next, we printed the value of both x and y. Here, the value of both variables will be changed to 10. The reason is we have stored the value 10 to the pointer of y where y is the address of x, so it will change the value of y and x to 10.
     
  • Now, we have again declared a pointer variable z and assigned it the address of pointer y.
     
  • We set the value 5 to the pointer of z.
     
  • In the end, we printed the value of x, y, and z.

Rules of Pointers in Carbon

As we all know, that Pointers in Carbon is a new topic, so there are many rules that you should know before writing any codes of Pointers in Carbon. Let's look at Pointers in Carbon's rules below in detail.

  • Pointers in Carbon do not support the pointer arithmetic.
     
  • There is no null pointer in the Carbon. So, we can use Optional(T*) to represent a pointer that does not refer to a valid object.
     
  • Pointers in Carbon support the dereference operator. When you have a pointer p as an input, *p returns the value that p points to as an l-value.
     
  • Pointers in Carbon also support the Address-of operator. When you have an l-value x as an input, &x will return a pointer to x.

Advantages of Pointers

Let's now discuss some advantages of Pointers in Carbon.

  • We can dynamically allocate and deallocate memory because of pointers.
     
  • We can increase the speed at which a program executes by using pointers.
     
  • Pointers allow us to modify and return several variables from a function.

Frequently Asked Questions

Who created Carbon?

Carbon was first proposed by Google employee Chandler Carruth at the CppNorth conference in Toronto in July 2022. He said that Carbon was developed as C++'s successor. A 1.0 release of the language is expected in 2024 or 2025. 

What will Carbon language be used for?

Carbon language will be used for the performance-sensitive software as it has code that is safe and easy to read.

Is carbon language garbage collected?

The interface between Carbon and C++ code is made to be simple and compatible with C++. Because of this, Carbon does not employ garbage collection and instead makes use of destructors.

What is the difference between Carbon and C++?

Carbon contains all the functionality of C++, but the inverse is invalid. The declarations start with the introducer keywords example, functions with fn, and variables with var. Imports in Carbon are more like the C++ modules.

How can we compile Carbon on windows?

You can use compiler explorer to compile carbon language on windows or install LLVM on windows and then set up the carbon environment.

Conclusion

In this article, we discussed the topic of Pointers in Carbon. In detail, we have seen the definition of the pointer. Later on, we discussed the rules of pointers in Carbon and some advantages of pointers.

We hope this blog has helped you enhance your knowledge of the Pointers in Carbon. If you want to learn more, then check out our articles.

And many more on our platform Coding Ninjas Studio.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass