I am a beginner in programming. It was taught that arrays store address of the first element. While using for loop when I input the Array elements using scanf I should not use & character right
it should be ("%d",Arr[i]) , instead of ("%d",&Arr[i]) .
but why it is showing error?
Array type has a special property, in some of the cases, a variable with type array decays to a type as pointer to the first element of the array.
Quoting C11, chapter §6.3.2.1
Except when it is the operand of the sizeof operator, the _Alignof 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. [...]
However, if the type is not an array type, it does not hold.
From your description, it sounds you have the array defined as
int Arr[16]; // 16 is arbitrary, just for example
In your case, Arr is an array of integers and Arr[i] is not an array type, it's an integer. So, you have to pass the address of this integer to scanf().
The correct statement would be
("%d",&Arr[i]); // passing the address.
To compare, if you have an array defined like
char array [16];
then you can write
scanf("%15s", array); // 'array' is array type, which is same as `&array[0]` in this case
Related
I was wondering why in C this is possible:
int MATRICE[20][20];
int *p; p = MATRICE[19]; is equal to p = &(MATRICE[19][0]);
I tried to interpretate it in this way: I consider the label "MATRICE" as a constant pointer and like an array of pointers, so when it comes to the 20th pointer (MATRICE[19]) it points at the same thing that MATRICE[19][0] points too.
Is my idea correct?
Arrays used in expressions with rare exceptions are converted to pointers to their first elements.
From 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.
This expression
MATRICE[19]
yields a one-dimensional array of the type int[20] that is implicitly converted to pointer to its first element of the type int * when used as an initializer (as a right side hand expression in the assignment) in this code snippet
int *p; p = MATRICE[19];
I am currently studying C language.
I wonder what 'array decaying' means, and when it happens.
And I wonder if the two variables below are interpreted in the same way.
char(*zippo)[2] = NULL;
char zippo2[4][2];
zippo = (char(*)[2])malloc(sizeof(char[2]) * 4);
From 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 two variables below
char(*zippo)[2] = NULL;
char zippo2[4][2];
have different types. The first one is a pointer to an object of the type char[2]. The second one is a two-dimensional array with four elements of the type char[2].
When the array zippo2 is used in expression except the expressions listed in the quote (as for example using it with the sizeof operator) then its designator is implicitly converted to pointer to its first element and has the same type as the variable zippo.
Why sizeof(array_name) is the size of the array and sizeof(&a[0]) is the size of pointer even though, when an array name is passed to a function, what is passed is the location of the beginning of the array.
In most expressions, when an array is used, it is automatically converted to a pointer to its first element.
There is a special rule for sizeof: When an array is an operand of sizeof, it is not automatically converted to a pointer. Therefore, sizeof array_name gives the size of the array, not the size of a pointer.
This rule also applies to the unary & operator : &array_name is the address of the array, not the address of a pointer.
Also, if an array is a string literal used to initialize an array, it is not converted to a pointer. The string literal is used to initialize the array.
The rule for this is C 2018 6.3.2.1 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.
Array parameters to functions don't exist. They turn into pointers (the innermost index does, if the array is multidimensional). It's just syntactic sugar inherited from the B language. void f(int *X) == void f(int X[]);
(Same for function params to functions. void g(void X(void)) == void g(void (*X)(void))).
Passing an array to a function, do it need to use &?
In below example Case 1 or Case 2, which one is good ?
#define IP_ADDR_LEN 4
char ip[4]={192,168,205,1};
char PeerIP[4];
int main()
{
memcpy(PeerIP,ip,IP_ADDR_LEN)
testip(PeerIP); /* Case 1 */
testip(&PeerIP); /*Case 2*/
}
int testip(char *ip)
{
/* IP check */
}
The first one.
testip(PeerIP);
is the right way to do it, as an array name will decay to a pointer to the first element of the array, which matches the expected type for the called function parameter.
To elaborate, in your code, testip() expects an argument of type char * and by calling it like testip(PeerIP); you are passing a char *, so no issues.
In case you use the &PeerIP, the type will be [char (*) [4]] pointer to array, not pointer to the first element of the array [char *] and they are different type.
Quoting C11, chapter §6.3.2.1, Lvalues, arrays, and function designators, (emphasis mine)
Except when it is the operand of the sizeof operator, the _Alignof 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. [...]
This question already has answers here:
In C, are arrays pointers or used as pointers?
(6 answers)
Closed 9 years ago.
When the array variable is considered as pointer and when it is considered as simple array in C? As example sometimes sizeof(array_variable) operator returns the address size and sometimes it returns the size of the array.
C 2011 (N1570) 6.3.2.1 3:
Except when it is the operand of the sizeof operator, the _Alignof 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.
C 2011 (N1570) 6.7.6.3 7:
A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation.
In a function argument, an array ([]) or [...]) is equivalent to a pointer (*). So sizeof(myarg) == sizeof(void*) is true for void f(char *myarg);, void f(char myarg[]); and void f(char myarg[42]);.
In global and local variables, an array is different from a pointer. sizeof(...) reflects that difference.
An array can always be converted to a pointer automatically (but not the other way round), and the address of the first element is used, i.e. ary is converted to &ary[0].
Always it is treated as pointer .
when you use array name without index , it will give the base address of the array . When you use the array name with index , it will be treated as *(array name + index) . It gives the indexe-th element from the base address . *array means first element .
name of an array variable is always a pointer. It is the specilization of sizeof() that it returns the size of an array if applied to an array variable.