could you tell my why the value of a referenced
array and the value of the array itself has the same value?
i know a is a type of int* but with &a it should be int** or am i wrong??
so the value should be a pointer to the a int pointer.
example code:
#include <stdio.h>
int main()
{
int a[10];
printf("a without ref.: 0x%x\n",a);
printf("a with ref.: 0x%x\n",&a);
return 0;
}
http://ideone.com/KClQJ
Name of the array decays to an pointer to its first element in this case.
Name of the array will implicit convert to a pointer ,except for two situation ,the one situations is "&array",the other is "sizeof(array)".In both cases,name of the array is a array ,not a pointer .
For example:
int a[10];
int *p;
p = a; //a is a pointer
p = &a; //a is a array,&a is a constant pointer
sizeof(a); //a is array
Given an array declaration T a[N], the expression &a has type "pointer to N-element array of T (T (*)[N]) and its value is the base address of the array. In that respect, the unary & operator behaves the same for arrays as it does for any other data type.
What's hinky is how C treats the array expression a. Except when it is the operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration, an array expression of type "N-element array of T" (T [N]) will be replaced with ("decay to") a pointer expression of type "pointer to T" (T *) and its value will be the address of the first element of the array. IOW, a == &a[0].
Since the address of the first element of the array is the same as the base address of the entire array, the expressions a and &a yield the same value, but the types are different (T * as opposed to T (*)[N]).
if there is int *p which point a, then,
a+0 == &a[0] == p+0 == &p[0] : address
*(a+0) == *(&a[0]) == *(p+0) == *(&p[0]) : data
a == &a == &a[0]
Related
#include<stdio.h>
void main()
{
char s[10][10];
int i;
for(i=0;i<4;i++)
scanf("%s",s[i]);
printf("%s",s);
printf("%s",s+1);
printf("%s",s[1]+1);
}
When I type the above line of code first printf statement will print the first string and second printf will print the second string since s[1] is equivalent to s+1. But the third printf will print the second string starting from the second character.
If s[1] is equivalent to s+1 why s[1]+1 does not give the result of s+2?
I do not get the idea of address calculation for 2D string array.
The way pointer arithmetic works, s[i] is equal to *(s + i). So s[1]+1 is actually *(s + 1) + 1, which is not the same as either s + 2 or *(s + 2).
So, let's talk about pointer arithmetic for a second. Given a pointer to any type T:
T *p;
the expression p+1 will evaluate to the address of the next object of type T. So if T is int, then p+1 will give us the address of the next int object after p. If p is 0x8000 and sizeof (int) is 4, then p+1 evaluates to 0x8004.
Where things get fun is if we're working with array expressions. Assume the following:
T a[N];
Except when it is the operand of the sizeof or unary & operators, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T" and its value will be the address of the first element of the array.
So if we write
int a[10];
int *p = a + 1;
the expression a is converted from "10-element array of int" to "pointer to int", and the value of a is the address of the first element (i.e., &a[0]). So the expression a + 1 gives us the address of the next integer object following a, which just happens to be &a[1]1.
Now assume we're working with a 2-D array:
int a[2][3];
int (*p)[3] = a + 1;
The expression a has type "2-element array of 3-element array of int". In this case, a "decays" to type "pointer to 3-element array of int", or int (*)[3]. So a + 1 gives us the address of the next *3-element array of int". Again, assuming a starts at 0x8000 and sizeof (int) is 4, then a + 1 evaluates to 0x800c
1. The expression a[i] is evaluated as *(a+i); that is, we're offsetting i elements from the address specified by a and dereferencing the result. Note that this treats a as a pointer, not an array. In the B language (from which C is derived), the array object a would have been a pointer object that contained the address of the first element (a[0]). Ritchie changed that in C so that the array expression would be converted to a pointer expression as necessary.
This question already has answers here:
How come an array's address is equal to its value in C?
(6 answers)
Closed 7 years ago.
The following program prints that a and array share the same address.
How should I understand this behavior?
Is it &arr the address for the pointer arr, which contains the beginning address the 10 chars?
#include <stdio.h>
int main()
{
char arr[10] = {0};
char* a = (char*)(&arr);
*a = 1;
printf("a=%p,arr=%p.\n", a, arr);
printf("%d\n", arr[0]);
return 0;
}
When you allocate an array in C, what you get is something like the following:
+---+
arr[0]: | |
+---+
arr[1]: | |
+---+
...
+---+
arr[N-1]: | |
+---+
That's it. There's no separate memory location set aside for an object named arr to store the address of the first element of the array. Thus, the address of the first element of the array (&arr[0]) is the same value as the address of the array itself (&arr).
Except when it is the operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T" and the value of the expression will be the address of the first element of the array.
So the type of the expression arr in the first printf call is char [10]; by the rule above, the expression "decays" to type char *, and the value is the address of arr[0].
In the expression &arr, arr is the operand of the unary & operator, so the conversion isn't applied; instead of getting an expression of type char **, you get an expression of type char (*)[10] (pointer to 10-element array of char). Again, since the address of the first element of the array is the same as the address of whole array, the expressions arr and &arr have the same value.
In idiomatic C, you should write char *a = arr; or char *a = &(arr[0]);. &arr is normally a char **. Even if modern (C++) compilers fixe it automatically, it is not correct C.
As arr is an array of char, arr[0] is a char and arr is the same as &(arr[0]) so it is a char *. It may be strange if you are used to other languages, but it is how C works. And it would be the same if arr was an array of any other type including struct.
The address printed out by arr and a is the memory address of the first element of the array arr. (Remember, the name of an array is always a pointer to the first element of that array.) This is because after you have defined the array arr, you define a as a pointer to the same address in memory.
According to the C Standard (6.3.2.1 Lvalues, arrays, and function designators)
3 Except when it is the operand of the sizeof operator or the unary &
operator, or is a string literal used to initialize an array, an
expression that has type ‘‘array of type’’ is converted to an
expression with type ‘‘pointer to type’’ that points to the initial
element of the array object and is not an lvalue. If the array object
has register storage class, the behavior is undefined.
The address of an array is the address of its first element. So though these types are different
char ( * )[10] ( that corresponds to &arr ) and char *(that is used in the casting in statement
char* a = (char*)(&arr); ) they will have the same value that is the address of the first element of the array.
If you would not use the casting then the correct definition of the pointer initialized by expression &arr would be
char ( *a )[10] = &arr;
The difference is seen then the pointer is incremented. For your definition of pointer a the value of expression ++a will be greater sizeof( char) than the initial value . For the pointer I showed the value of expression ++a will be greater 10 * sizeof( char ) than the initial value.
In your case the type of expression *a is char and you may write *a = 1; while in my case the type of expression *a will be char[10] and you may not write *a = 1;
Here,I have some Doubt with the output.
Why the Output is same ?
int (*r)[10];
printf("r=%p *r=%p\n",r,*r);
return 0;
Platform- GCC UBUNTU 10.04
Because Name of the array decays to an pointer to its first element.
int (*r)[10];
Is an pointer to an array of 10 integers.
r gives you the pointer itself.
This pointer to the array must be dereferenced to access the value of each element.
So contrary to what you think **r and not *r gives you access to the first element in the array.
*r gives you address of the first element in the array of integers, which is same as r
Important to note here that:
Arrays are not pointers
But expressions involving array name sometimes behave as pointer when those being used as name of the array would not make sense.
You would better understand if you look at the following program.
#include <stdio.h>
int main()
{
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int (*r)[10] = &a;
printf("r=%p *r=%p *(r+0)=%p *(r+1)=%p\n", r, *r, *(r+0), *(r+1));
printf("sizeof(int)=%d \n", sizeof(int));
return 0;
}
The output is as follows:
r=0xbfeaa4b4 *r=0xbfeaa4b4 *(r+0)=0xbfeaa4b4 *(r+1)=0xbfeaa4dc
sizeof(int)=4
Observations / Point(s)-to-note:
_DO_NOT_ de-reference a pointer which has not yet made to point to an address. So in your program int (*r)[10]; was de-referenced without being assigned to a memory area. This is not acceptable.
If you see the output - *r is same as *(r+0) which is same as r (only w.r.t this case)
If you see the output for *(r+0) and *(r+1) it is 40 bytes (0xbfeaa4dc - 0xbfeaa4b4 = sizeof(int) * size of the array (which is 10 in this case). So when you increment a pointer to a particular type, it gets incremented to sizeof(type) bytes!
the other worth-notable points about a pointer-to-an-array-of-integers are explained here
Hope this helps!
Remember that when an expression of type "N-element array of T" appears in most contexts, it will be converted to an expression of type "pointer to T" and its value will be the address of the first element in the array. The exceptions to this rule are when the array expression is an operand of either the sizeof or unary & (address-of) operands, or if the array expression is a string literal being used as an initializer in an array declaration.
Your situation is a mirror image of the following:
int a[10] = {0};
printf("a = %p, &a = %p\n", (void *) a, (void *) &a);
In the printf call, the expression a has its type converted from "10-element array of int" to "pointer to int" based on the rule above, and its value will be the address of the first element (&a[0]). The expression &a has type "pointer to 10-element array of int", and its value will be the same as a (the address of the first element in the array is the same as the address of the array itself).
Your code has a bit of undefined behavior in that you're dereferencing r before it has been assigned to point anywhere meaningful, so you can't trust that the output is at all accurate. We can fix that like so:
int a[10] = {0};
int (*r)[10] = &a;
printf("r = %p, *r = %p\n", (void *) r, (void *) *r);
In this case, r == &a and *r == a.
The expression r has type "pointer to 10-element array of int", and its value is the address of a. The expression *r has type "10-element array of int, which is converted to "pointer to int", and its value is set to the address of the first element, which in this case is a[0]. Again, the values of the two expressions are the same.
I have this function that takes a pointer of an array (in order to modify it from within the function)
int func_test(char *arr[]){
return 0;
}
int main(){
char var[3];
func_test(&var);
return 0;
}
When I try to compile this I get :
passing argument 1 of ‘func_test’ from incompatible pointer type
Why is this problem, and how I pass a pointer to that array in this case?
char * arr[] is not a pointer to an array; it is an array of pointers. Declarations in C are read first from the identifier towards the right, then from the identifier towards the left. So:
char * arr[];
// ^ arr is...
// ^ an array of...
// ^ pointers to...
// ^ char
A pointer to an array is a type (*varname)[], in your case, a char (*arr)[].
You are passing the address of a pointer. I think you want this:
int func_test(char arr[]){
arr[0] = 'a';//etc.
return 0;
}
int main(){
char var[3];
func_test(var);
return 0;
}
char var[3] is an array that holds 3 characters, not 3 pointers to characters - char *arr[] denotes an array that holds pointers to characters.
So you can go like this:
char *var[3];
func_test(var);
Note that the ampersand is not needed because array identifiers automatically decay to pointers of the corresponding type, in this case char **.
That's because the name of an array is already a pointer to it, so use func_test(var).
&var will have type char**
This is an array of pointers
char *arr[]
This is an address of a pointer
&var
So you are passing something different to what the function expects
C's treatment of arrays is a little confusing at first.
Except when it's an operand of either the sizeof or unary & operators, or when it's a string literal being used to initialize another array in a declaration, an expression with type "N-element array of T" will be implicitly converted to type "pointer to T", and its value will be the address of the first element in the array.
Assume the following declaration:
int x[10];
The type of the expression x is "10-element array of int". However, when that expression appears as, say, a parameter to a function:
foo(x);
the type of x is implicitly converted ("decays") to type "pointer to int". Thus, the declaration of foo needs to be
void foo(int *p);
foo receives an int *, not an int [10].
Note that this conversion also occurs for something like
i = x[0];
Again, since x isn't an operand of sizeof or &, its type is converted from "10-element array of int" to "pointer to int". This works because array subscripting is defined in terms of pointer arithmetic; the expression a[n] is equivalent to *(a+n).
So for your code, you'd write
int func_test(char *arr) { return (0); }
int main(void)
{
char var[3];
func_test(var); // no & operator
return 0;
}
Postfix operators like [] have higher precedence than unary operators like *, so a declaration like T *a[N] is interpreted as T *(a[N]), which declares an array of pointer to T. To declare a pointer to an array, you have to use parentheses to explicitly group the * operator with the array name, like T (*a)[N].
Here's a handy table of array declarations, expressions, and types:
Declaration: T a[N]; // a is an N-element array of T
Expression Type Decays To
---------- ---- ---------
a T [N] T *
&a T (*)[N]
a[i] T
&a[i] T *
Declaration: T *a[N]; // a is an N-element array of pointer to T
Expression Type Decays To
---------- ---- ---------
a T *[N] T **
&a T *(*)[N]
a[i] T *
*a[i] T
&a[i] T *
Declaration: T (*a)[N] // a is a pointer to an N-element array of T
Expression Type Decays To
---------- ---- ---------
a T (*)[N]
&a T (**)[N]
*a T [N] T *
(*a)[i] T
This might be a stupid question, but I have a little problem with understanding of C Pointers. Even more when it comes to arrays. For example:
char ptr[100];
ptr[0]=10;
fprintf(stderr, "&ptr: %p \n ptr: %p \n*ptr: %d\n", &ptr, ptr, *ptr);
if ( &ptr == ptr ) {
fprintf(stderr, "Why?\n");
}
How is this even possible? 'ptr' is at the adress &ptr. And the content of ptr is the same as &ptr. Then why is *ptr = 10 ???
The address of the first element of the array is the same as the address of the array itself.
Except when it is the operand of the sizeof or address-of & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be implicitly converted ("decay") to type "pointer to T" and the value will be the address of the first element in the array.
If the expression a is of type "N-element array of T", then the expression &a is type "pointer to N-element array of T", or T (*)[N].
Given the declaration
T a[N];
then the following are all true:
Expression Type Decays to
---------- ---- ---------
a T [N] T *
&a T (*)[N] n/a
*a T n/a
The expressions a and &a both evaluate to the same value (the location of the first element in the array), but have different types (pointer to T and pointer to array of T, respectively).
ptr (which, as sbi says, is really an array) decays to &(ptr[0]) (char * to first element)
This is the same address as &ptr (a char (*) []), even though they are different types.
int arr[5];
arr[0]= 7;
fprintf(stdout,"%p %p %d",&arr[0],arr,*arr);
if( (int)&arr == (int)arr ) printf("good\n");
else printf("bad\n");
return 0;
}
This will work....