Array name as pointer and array name with & operator [duplicate] - c

This question already has answers here:
Difference between pointer to pointer and pointer to array?
(3 answers)
Closed 9 years ago.
Whats the difference between last 2 statements:
int a[20];
int *b=a;
int *c=&a;
I think both are same, but in a recent interview the interviewer was keen to know the difference which I didn't know.
Can somebody please explain with detail example.
I went to this post but didn't understand the array related part:
Function pointers and address of a function

int a[20];
You are declaring an array a which can hold 20 int
int *b=a;
Since a is an array, a evaluates to the base address of array a (i.e. first element of a). So b is an int pointer which points to the first element in array a
int *c=&a;
&a means address of a which is being assigned to int pointer c. But, this is not good as &a is address of an array (not the base address of the array), so you should define c as int ** (i.e. pointer to a pointer) or assign it to &a[0] (i.e. the address of the first element of array a). Since array is assigned contiguous memory, once you have the base address of the array, you can easily access the remaining elements of the array by doing *(b+i) where i is an index of the element you want to access.
However,
int a = 5;
int *b= a ; // WRONG -- b will not point to the address of variable a, but to the memory location 5, which is what you do not want
int *c = &a; //CORRECT

Related

Does arrays in C takes separate memory to store the address of the first element? [duplicate]

This question already has answers here:
How come an array's address is equal to its value in C?
(6 answers)
Closed 1 year ago.
In many tutorial videos and explanations of C programming, for representing arrays like
int a[5] = {9,2,1,3,4};
Tutors/teachers always make a separate box for storing the address of first element of array i.e address of 9 in this case.
Which means that the total memory consumed by the array should be (memory consumed by the constant pointer a to store the address of first element + 5*(sizeof(int)).
If that's the case then why this program which is mentioned below gives such an output.
Program
#include <stdio.h>
int main()
{
int a[5] = {6,1,2,9,3};
int * p = a;
printf("a is %u\n", a);
printf("&a is %u\n", &a);
printf("&a+1 is %u\n", &a+1);
printf("p is %u\n", p);
printf("&p is %u", &p);
return 0;
}
Output
a is 2128206832
&a is 2128206832
&a+1 is 2128206852
p is 2128206832
&p is 2128206824
Here a and &a give the same address.
Which shouldn't be the case at all when looking at the representation mostly used by the people to represent arrays in C.
According to their diagram representation of array, it should have given different values for a and &a, just like p and &p.
So how array is truly represented inside of the memory?
Sorry for such a trivial doubt, but I am confused.
And does a create a separate memory space to store the address of first element of array?
If yes then why a and &a is giving same value?
It should have differed just like p and &p.
No, a does not create a separate memory to store the address of array. &a gives address of array and a gives address of a[0]. Both &a and a are same a because the address of the first element is same as address of the array. int a[5] does precisely what it looks like , generates array of 5 elements. When you create p , you create a separate variable which has a value that &a[0]. You may look into this for more clarity. You should also look at this

How can array name and the address of the array name both prints the same value? [duplicate]

This question already has answers here:
Is an array name a pointer?
(8 answers)
How come an array's address is equal to its value in C?
(6 answers)
C: Does the address operator (&) produce a pointer (address + type) or just an address?
(3 answers)
Closed 4 years ago.
Consider the following code snippet.
int main(){
int x[2];
x[0]=100;
x[1]=200;
printf("%d\n",x);
printf("%d\n",&x);
printf("%d\n",*x);
}
The output is given as (in three lines)
6487616 6487616 100
I've read that the array name is a pointer to the first element of the array. Therefore, the value of 'x' prints a memory address. But if i try to print the address of the pointer which holds the address of the first element of the array, why does that also print the same memory address? Shouldn't it be printing another memory address, simply because in order for a variable to store a memory address it should be a pointer and that pointer variable also has a memory address in the RAM.
int main(){
int y = 10;
int *p = &y;
printf("%d\n",&y);
printf("%d\n",&p);
}
The above code gives the output as
6487628 6487616
So why doesn't this work when it comes to arrays?
Contrary to what you might have heard, arrays and pointers are not the same thing. An array is a sequence of one or more elements of a given type, while a pointer points to a memory location (which may be the first element of an array). Because of this, the address of an array is the same as the address of the first element.
Where the confusion comes in is that in most expressions, an array decays into a pointer to the first element. One of the times this decaying does not happen is when the array is the subject of the address-of operator &.
In your first piece of code, you first print x. Ignoring for the moment that you should be using %p to print a pointer instead of %d, here x decays into a pointer to the first element and that address is what is printed. In the next call to printf, you pass in &x. This has the same value as x (when converted to a pointer) but has a different type, i.e. x has type int [2] (which decays to int *) and &x has type int (*)[2].
In your second example, y is an int and p is an int *. These are separate variables, so each has a different address.
First, for printing memory addresses, one should use %p instead of %d; using %d is actually undefined behaviour, even if it often works:
int main(){
int x[2];
x[0]=100;
x[1]=200;
printf("%p\n",(void*)x);
printf("%p\n",(void*)&x);
printf("%d\n",*x);
}
Second, &x does not introduce a new object / variable but uses the address of object x. The use of x decays to a pointer to the first element of array object x, and this is equivalent to &x and &x[0]. So same object -> same address, and therefore, x, &x, and &x[0] give you the same address.
In the other example, you introduce a new object p, which is different from object x. That's why &p and &x give different addresses
What you heard is slightly incorrect.
An array is not "a constant pointer". An array is an array.
When you use an array in an expression, what you get is a pointer to the array's first element.
That pointer will have the same value (although a different type) as a pointer to the whole array.
These facts explain the results you saw.
You should use %p to print memory address
#include <stdio.h>
int main(){
int y = 10;
int *p = &y;
printf("%p\n",&y);
printf("%p\n",&p);
}
output :
0x7ffeed2dd6fc
0x7ffeed2dd6f0
by the way
int main(){
int x[2];
x[0]=100;
x[1]=200;
printf("%p\n",x); // u should use %p here
printf("%p\n",&x); // this is the address of the array same as upper
printf("%d\n",*x); // this is the value of x it's same as x[0]
}

Can arrays of pointers & 2-D array consider as same [duplicate]

This question already has answers here:
Difference between pointer to pointer and pointer to array?
(3 answers)
Closed 5 years ago.
Can anyone please tell if both the entity - i.e.
2-D array char array[][9] and
array of pointers char *array[9] are same or different??
They are different.
The most prominent difference is that in the case of the array the elements will be laid out contigously in memory but in the case of the array of pointers they will not.
To be able to use the elements of the array of pointers, you need to make them point to some valid memory, you can do that either by using malloc() or by pointing to an array for example. In both cases the elements of the potential array the pointer points to are not contiguous.
Your confusion might be due to the fact that arrays can be syntactically used as pointers, because if you pass an array to a function expecting a pointer then, it is equivalent to a pointer that points to the first element of such array.
Also,
int x[3] = {1, 2, 3};
int *y = x;
is valid and you can use y as if it was an array even though it's not. It's a pointer, and you can use it to traverse the elements of the array x using pointer arithmetic.
One way of expressing such arithmetic is by the use of the [] syntax, in fact
int value = y[1]
is equivalent to performing a pointer arithmetic operation on y and dereferencing it, exactly equivalent to
int value = *(y + 1);
i.e. move to the element that is (1×sizeof *y) bytes after the beginning of y and get the value there.

What is pointer to array of integers

In this example, what it means? In my opinion in this, to all the address of array b[] ,array a[] will point correspondingly to all its location ? So do we write it in the following way?
int (*a)[10];
int b[10];
a = &b;
a is a pointer to an array of 10 ints. a=&b; means that a is pointing to array b, i.e, It contains the address of the first byte of b which is the address of array b.
It means a is a pointer to an array of 10 ints. The declaration like this where number of elements is specified is used to make pointers to two-dimensional arrays. Like
int (*a)[10];
int b[][10];
a=b;
If you are creating a pointer to one dimensional array, declaration like this is not necessary. If you have b an array of 10 ints and you want to make a pointer to it simply do this
int *a;
int b[10];
a=b;
Also & is not necessary on arrays because b is already the address of first element in the array. & is only necessary when you want an address of specific member of the array like int *a=&b[5];
a is a pointer to an array of 10 integers.
So, for example, a++ will increase a
by the size of int(ie = 4 bytes)

address of array ptr equal to to it's value? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C: How come an array’s address is equal to its value?
SA
In C I tried to print the address of the pointer of an array.
int a[3] = {0,1,2};
printf("\n%p",a);
printf("\n%p",(&a));
the 2 statement prints the same value why?thanks in advance
Any array argument to a function will decay to a pointer to the first element of the array.
The C Book has an excellent explanation of this:
Another case is when an array name is
the operand of the & address-of
operator. Here, it is converted into
the address of the whole array. What's
the difference? Even if you think that
addresses would be in some way ‘the
same’, the critical difference is that
they have different types. For an
array of n elements of type T, then
the address of the first element has
type ‘pointer to T’; the address of
the whole array has type ‘pointer to
array of n elements of type T’;
clearly very different
In C arrays "decay" to pointers to their first element in certain contexts. Passing an array to a function is one of these cases.
Because local arrays are treated as a special case (they decay to pointers).
In your case &a is translated to a.
If you try with an int pointer, e.g.,
int c = 3;
int* p = &c;
you should get different values.
An array "decays" into a pointer to it's elements in some situations. Passing it as an argument to a function is one of those. Thus in
void func(int * pi, size_t nints);
int a[3] = {0, 1, 2};
func(a, 3);
the pointer to a is passed to func. This is what happens in your code as well. This also happens when you assign it to a pointer:
int * pi = a;
assert(*pi == 0); assert(*(pi + 1) == 1); assert(*(pi + 2) == 2);
The asserts here would also work if you replace pi with a.

Resources