If I have the following pointer variable declarations:
int *a;
int **c;
Regarding to the type and what values each will hold, will it be as follows:
a is of type int*, and will hold a memory address
*a is of type int, and will hold the value of the variable the pointer is pointing to
c is of type int**, and will hold ?????????????????????
c* is of type int*, and will hold the memory address of the pointer it is pointing at
c** is of type int, and assuming that pointer c is pointing to pointer b, and pointer b is pointing to variable a, here, the value held will be the value of the variable a
Is it correct this way, except c which I'm not sure about?
Thanks.
int *a;
int **c;
You are correct about a. It is more common to say a holds a pointer to int.
c is of type int** and will hold a pointer to a pointer to int.
*c is of type int*, and will hold a pointer to int.
**c is of type int, and will hold an integer value. You are correct about c pointing to b and b pointing to a.
See cdecl for some help. :)
c is of type int**, and will hold ?????????????????????
'c' also holds a memory address, just as 'a' does. The difference is that 'c', when dereferenced, will return another memory address. You're just adding another level of indirection.
In your example, c will hold a pointer to an int*; that is, c is a pointer to a pointer. It can be used for multidimensional arrays (like matrices) and it can be used as a function parameter to change a user's int*.
-- a is of type int*, and will hold a memory address
Correct
-- *a is of type int, and will hold the value of the variable the pointer is pointing to
Not exactly: *a will be a reference to the variable the adress points to. You see this when trying *a = 8; and int * x = &(*a)).
If it was a value you couldn't change it. But since it is a reference the value is "routed" to the original place...in this case the memory a points to.
-- c is of type int**, and will hold ?????????????????????
c holds a memory address pointing to a memory address pointing to an int.
*c holds a reference to a memory adress pointing to an int. So you can do: *c = a;
**c is the same as *a.
Every pointer holds memory address. In this case, c is a pointer to int* so, it will hold a memory address of such a variable.
Often, double pointers are used to create dynamic multiarrays in C. You can see it here
C is of type int*. As evident from that, it will hold a data of type int* which in itself is a memory address.
This concept is called far pointer and there can be multiple far pointers up to a certain limit.
Like int** c, you can also have int*** d point to int** c. This is like a queue with every pointer pointing to the next pointer and the front having the data as the actual variable.
The simplest thing that I can think of is playing with spaces. The compiler does not really care, but it makes reasoning easier due to the fact that the syntax of declarations and the usage are the same (by design):
Given:
int ***a; // declaration
int*** a; // type of a is an int*** (read: pointer to pointer to pointer to int)
int** *a; // the type of the object pointed by a (*a) is int**
int* **a; // the type of **a is int*
int ***a; // the type of ***a is int
Disclaimer: this is with respect to data types. Whether you can or not dereference a pointer at runtime is a different issue (has it been initialized, does it point to valid memory...?)
Related
Is pointer a variable or an address?
Because int** array= new int*[4] is so confusing.
For int** array in this code, the pointer becomes variables to store the pointer addresses, and new int*[4] becomes addresses that will be stored in another pointer.
What am I understanding wrong?
Pointer is a data type, similar to int or char. The data here is a memory address. Pointers are qualified with the types of values expected to be located in the memory at the referred addresses. As other data types, pointers could be used as types for variables, function arguments, array elements etc.
So, int **array is a variable of type int ** which means a pointer to a value of type int * located somewhere in the memory. In turn, int * means a pointer to a value of type int located somewhere in the memory. Or, in short, int ** is a pointer to a pointer to an int.
Expression new int*[4] means allocate in the memory an object of type int *[4] and return a pointer to it. Type int *[4] means an array of four elements of type int * where int *, as we already know, means a point to int value.
So, in the initialization, the types of the left part (int **) and of the right part (int *[]) are different. However, C++ is able to automatically convert arrays into pointers in case the type of array element is the same as the type of the value referred by the pointer. In our case, array element type is int * and the same is the type referred by the pointer. When an array is converted to a pointer, the resulting pointer will refer to the first element of the array.
In this declaration
int** array= new int*[4];
array is a variable that is initialized by the address of the first element of a dynamically allocated array with the element type int *.
If you have an array declared like
T array[N];
where T is some type then a pointer to the first element of the array can be declared like
T *p = array;
If to take into account your array then just T is equal to int *.
Simply put, a pointer is a variable that can store the address of another variable instead of an int or a float or some other value like a "normal" variable.
Like any other variable, the pointer itself also has an address so you can have a pointer to pointer, i.e. a pointer that stores the address of another pointer.
I'm new to programming and I'm trying to wrap my head around the idea of 'pointers'.
int main()
{
int x = 5;
int *pointerToInteger = & x;
cout<<pointerToInteger;
}
Why is it that when I cout << pointerToInteger; the output is a hexdecimal value, BUT when I use cout << *pointerToInteger; the output is 5 ( x=5).
* has different meaning depending on the context.
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.
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
A multiplication operator.
int i = 10*20; // Needs no explanation.
One way to look at it, is that the variable in your source/code, say
int a=0;
Makes the 'int a' refer to a value in memory, 0. If we make a new variable, this time a (potentially smaller) "int pointer", int *, and have it point to the &a (address of a)
int*p_a=&a; //(`p_a` meaning pointer to `a` see Hungarian notation)
Hungarian notation wiki
we get p_a that points to what the value &a is. Your talking about what is at the address of a now tho, and the *p_a is a pointer to whatever is at the &a (address of a).
This has uses when you want to modify a value in memory, without creating a duplicate container.
p_a itself has a footprint in memory however (potentially smaller than a itself) and when you cout<<p_a<<endl; you will write whatever the pointer address is, not whats there. *p_a however will be &a.
p_a is normally smaller than a itself, since its just a pointer to memory and not the value itself. Does that make sense? A vector of pointers will be easier to manage than a vector of values, but they will do the same thing in many regards.
If you declare a variable of some type, then you can also declare another variable pointing to it.
For example:
int a;
int* b = &a;
So in essence, for each basic type, we also have a corresponding pointer type.
For example: short and short*.
There are two ways to "look at" variable b (that's what probably confuses most beginners):
You can consider b as a variable of type int*.
You can consider *b as a variable of type int.
Hence, some people would declare int* b, whereas others would declare int *b.
But the fact of the matter is that these two declarations are identical (the spaces are meaningless).
You can use either b as a pointer to an integer value, or *b as the actual pointed integer value.
You can get (read) the pointed value: int c = *b.
And you can set (write) the pointed value: *b = 5.
A pointer can point to any memory address, and not only to the address of some variable that you have previously declared. However, you must be careful when using pointers in order to get or set the value located at the pointed memory address.
For example:
int* a = (int*)0x8000000;
Here, we have variable a pointing to memory address 0x8000000.
If this memory address is not mapped within the memory space of your program, then any read or write operation using *a will most likely cause your program to crash, due to a memory access violation.
You can safely change the value of a, but you should be very careful changing the value of *a.
yes the asterisk * have different meanings while declaring a pointer variable and while accessing data through pointer variable. for e.g
int input = 7;
int *i_ptr = &input;/*Here * indicates that i_ptr is a pointer variable
Also address is assigned to i_ptr, not to *iptr*/
cout<<*i_ptr;/* now this * is fetch the data from assigned address */
cout<<i_ptr;/*it prints address */
for e.g if you declare like int *ptr = 7; its wrong(not an error) as pointers ptr expects valid address but you provided constant(7). upto declaration it's okay but when you go for dereferencing it like *ptr it gives problem because it doesn't know what is that data/value at 7 location. So Its always advisable to assign pointers variable with valid addresses. for e.g
int input = 7;
int *i_ptr = &input;
cout<<*i_ptr;
for example
char *ptr = "Hello"; => here * is just to inform the compiler that ptr is a pointer variable not normal one &
Hello is a char array i.e valid address, so this syntax is okay. Now you can do
if(*ptr == 'H') {
/*....*/
}
else {
/*.... */
}
what means the two stars in this c++ code, I try to convert it in objective c but I never saw that how can I do that?
int **gFFTBitTable = NULL;
Thanks for advance
In both Objective C and C++ it is valid. It is pointer to pointer. As, pointer can keep address of other variable, pointer to pointer can keep address of pointer.
Example,
int i;
int *p = &i; //p is pointer
int **q = &p; //q is pointer to pointer
It means it's a pointer of pointers. If a pointer can be used like an array to keep track of say 10 integers (a row) a pointer of pointers could keep track of say 10 of those pointers (a table)
Why is
int *a = new int[10];
written as
int *a;
a = new int[10];
instead of
int *a;
*a = new int[10];
?
The way I see it, in the second block of code you're saying a, which was a pointer variable, is now an array. In the third block of code you're saying the thing a points to is now an array. Why does the second make more sense than the third?
new int[10] returns a pointer to the first element in the array (which is of type int*). It does not return a pointer to the array (which would be of type int(*)[10]).
a = new int[10] means make a point to the first element of the dynamically allocated array. *a is not a pointer at all. It is the object pointed to by the pointer a (which is of type int).
Note that if you actually had a named array object, the syntax would still be the same:
int x[10];
int* a = x;
Why? In C++, in most cases, whenever you use an array, it is implicitly converted to a pointer to its initial element. So here, int* a = x is the same as int* a = &x[0];.
(There are several cases where the array-to-pointer decay does not occur, most notably when the array is the operand of the & or sizeof operators; they allow you to get the address of the array and the size of the array, respectively.)
*a = ... means assign value to memory that I'm pointing to
a = ... means assign me new memory address that I should point to
The type int* is a pointer to an int. If you apply the derefence operator to such a pointer, you get an int (or, more precisely, actually a reference to an int, i.e. a int&) That is, when you write
int* a;
*a = new int[10]; // ERROR: incompatible types `int&` and `int*`
you try to assign the result of new int[10] which is of type int* to an object of type int&. This isn't supposed to work.
In the third block of code you're saying the thing a points to is now an array.
You're not, though.
new doesn't evaluate to an array, but to a pointer to a block of memory. That's why you're assigning to a pointer.
It's a consequence of dynamically-allocated objects not being directly reachable in the current lexical scope.
Because this :
a = new int[10];
and this :
*a = new int[10];
are completely different things. The 2nd is the pointer dereferencing.
This:
int *a = new int[10];
and this
int *a;
a = new int[10];
are same things.
You are assigning to a pointer variable, so you have to write a.
Would you have written
int* a = new int[10];
this would be more obvious that the type of a is int* (pointer to an `int).
Because operator new returns the address of the object(s) it creates. *a is not an address, it's the int that a points at.
When you declare a pointer,
int *a;
in the memory:
Memory Address Variable Identifier Variable Value
-------------- -------------- --------------
0xAE934904 a ?
When you create a dynamic array, you have 10 integer numbers:
a = new int[10];
Memory Address Variable Identifier Variable Value
-------------- -------------- --------------
0xH3948300 - ?
0xH3948304 - ?
0xH3948308 - ?
0xH394830C - ?
...
(in total, 10 integers) and value of a becomes 0xH3948300.
Memory Address Variable Identifier Variable Value
-------------- -------------- --------------
0xAE934904 a 0xH3948300
On the other hand, if you write *a = new int[10]; this is *(?) which means go to the value that is stored at memory location ?. *(?) is probably not a memory space where you can store memory address. If you want to assign a value to the pointer, you simply type the identifier name, a.
I don't understand what the datatype of this is. If its a pointer or an array. Please explain in simple terms. To quote what was in the book-
If you want to pass an array of pointers into a function, you can use the same method that you use to pass other arrays—simply call the function with the array name without any indexes. For example, a function that can receive array x looks like this:
void display_array(int *q[])
{
int t;
for(t=0; t<10; t++)
printf("%d ", *q[t]);
}
Remember, q is not a pointer to integers, but rather a pointer to an array of pointers to
integers. Therefore you need to declare the parameter q as an array of integer pointers,
as just shown. You cannot declare q simply as an integer pointer because that is not
what it is.
cite: C++: The Complete Reference, 4th Edition by Herbert Schildt, Page 122-123
This is how it's built up:
int is the type "int".
int* is the type "pointer to int"
int* [] is the type "array (of unknown bound/length) of pointer to int"
int* p[] is the declaration of a variable or parameter named p of the type above.
... pointer to an array of pointers to integers
No it's not. q is the type int *[]. Which is an invalid (or possibly incomplete, depending on context) type in C++, and only valid in some places in C. Arrays must have a size.
The type int *[] is an (unsized) array of pointers to int. It is itself not a pointer.
The confusion probably comes from the fact that an array can decay to a pointer to its first element.
For example, lets say we have this array:
int a[20];
When plain a is used, it decays to a pointer to its first element: a is equal to &a[0].
int *p[]
// ^
p is
int *p[]
// ^^
p is an array of unspecified size (possibly illegal, depends on context)
int *p[]
// ^^^^^
p is an array of unspecified size of pointers to int
Meaning each element of p is a pointer:
int foobar = 42;
p[0] = NULL;
p[1] = &foobar;
I don't understand what the datatype of this is
If it's any comfort, neither does the author of the book you are reading.
Remember, q is not a pointer to integers, but rather a pointer to an array of pointers to integers.
This is bullschildt.
Before adjustment of parameters, q is an array of pointers to integers.
After adjustment of parameters, q is a pointer to the first element of an array of pointers to integers. Equivalent to int** q, a pointer to pointer to an int.
Nowhere is it "a pointer to an array of pointers to integers". That would have been int* (*q)[].
I would advise to stop reading that book.
The key here is that any array that is part of a parameter list of a function, gets adjusted ("decays") into a pointer to the first element. So it doesn't matter if you type int* q[666] or int* q[], either will be silently replaced by the compiler with int** "behind the lines".
This is actually the reason why we can write [] in a parameter list - normally an empty array would be an incomplete type that can't be used before completion elsewhere. But since parameters always get adjusted, they are never of array type, and it doesn't matter that the original type was incomplete.