What Exactly Is the Purpose of the (Asterisk) in Pointers

What exactly is the purpose of the (asterisk) in pointers?

* has different meaning depending on the context.

  1. Declaration of a pointer

    int* ap;  // It defines ap to be a pointer to an int.

    void foo(int* p); // Declares function foo.
    // foo expects a pointer to an int as an argument.
  2. Dereference a pointer in an expression.

    int i = 0;
    int* ap = &i; // ap points to i
    *ap = 10; // Indirectly sets the value of i to 10
  3. A multiplication operator.

    int i = 10*20; // Needs no explanation.

Pointers in C: when to use the ampersand and the asterisk?

You have pointers and values:

int* p; // variable p is pointer to integer type
int i; // integer value

You turn a pointer into a value with *:

int i2 = *p; // integer i2 is assigned with integer value that pointer p is pointing to

You turn a value into a pointer with &:

int* p2 = &i; // pointer p2 will point to the address of integer i

Edit:
In the case of arrays, they are treated very much like pointers. If you think of them as pointers, you'll be using * to get at the values inside of them as explained above, but there is also another, more common way using the [] operator:

int a[2];  // array of integers
int i = *a; // the value of the first element of a
int i2 = a[0]; // another way to get the first element

To get the second element:

int a[2]; // array
int i = *(a + 1); // the value of the second element
int i2 = a[1]; // the value of the second element

So the [] indexing operator is a special form of the * operator, and it works like this:

a[i] == *(a + i);  // these two statements are the same thing

What does the asterisk in C do?

This type of * is called "indirection operator", and *test means "get the data from where the pointer test points".

char is reserved for use as a keyword, so char = *test won't compile unless char is defined as a macro.

What does an asterisk (*) after a variable name mean in opengl?

That would be the c++ notation for a pointer.

Source: http://www.cplusplus.com/doc/tutorial/classes/#pointers_to_classes

Placement of the asterisk in pointer declarations

4, 5, and 6 are the same thing, only test is a pointer. If you want two pointers, you should use:

int *test, *test2;

Or, even better (to make everything clear):

int* test;
int* test2;

why pointer variable with asterisk and without asterisk behave differently in printf?

When you declare char string[]="Hello" you declare an array of characters.

string points to the first element of the array. * is known as the dereferencing operator.

Suppose ptr is an integer pointer. *ptr will give you the content of the memory location pointed by the pointer ptr. At the time of declaration you have to declare it as int *ptr so that the compiler knows that ptr is a pointer variable(hence the asterix there).


when you execute printf("first character is %c", *my_pointer); the function expects a character corresponding to %c. The declaration char *string and char string[] are roughly(not completely) equivalent. my_pointer is a character type pointer. *my_pointer dereferences my_pointer. In other words *my_pointer gives you the content of my_pointerie the first character in your string.


However a %s expects a string (a character array, which is basically a char pointer) as the corresponding argument in printf. *my_pointer is a character. my_pointer can be treated as a string. Also print will treat the argument as a string and print the who;e thing till \0.


my_pointer=array_of_words "assigns" the value of array_of_words to my_pointer.

array_of_words[] is an array, so the value of array_of_words is basically the memory address of the first element of the array(notice the absence of []). In effect my_pointer now points to the first element of the array.

Hope this explains it.

Type Declaration - Pointer Asterisk Position

Stroustrup was asked this and he said (paraphrasing)

  • if you think more C-ish you will say int *a and Employee *pE
    (so in your head you're thinking "the content of a is an integer")
  • if you think more C++-ish you will say int* a and Employee* pE
    (so in your head it's "a is an integer pointer")

You can think however you like, as long as you never declare two
pointers on the same line.

Works for me. I'm an Employee* pE kind of person, but I'm married to an Employee *pE kind of person - my advice would be not to get too worked up about it.

What does the asterisk do in Go?

Im guessing it means the same as in C

p is a pointer to a string

The statement var p *string = &s would assign the address of the s object to p

Next line *p = "ciao" would change the contents of s

See this link from the Language Design FAQ

Interestingly, no pointer arithmetic

Why is there no pointer arithmetic?
Safety. Without pointer arithmetic
it's possible to create a language
that can never derive an illegal
address that succeeds incorrectly.
Compiler and hardware technology have
advanced to the point where a loop
using array indices can be as
efficient as a loop using pointer
arithmetic. Also, the lack of pointer
arithmetic can simplify the
implementation of the garbage
collector.



Related Topics



Leave a reply



Submit