This question already has answers here:
Is there a reason why an array name is not an lvalue?
(4 answers)
Is an array name a pointer?
(8 answers)
Closed 6 years ago.
#include <stdio.h>
int main(void) {
int values[10];
int a = 10;
values = &a;
printf ("the value is = %i.\n\n", *values);
return 0;
}
This code is written just for experimenting on pointers, I have just started learning it.
My question is that if the name of the array is a pointer then why cant we copy some other variable's address into it.
The error that it gave was "assignment to expression with array type"
please explain it in simple way.
Arrays cannot be assigned to. You can store values into array elements and you can store array addresses into pointers, but arrays themselves cannot appear on the left side of an assignment operator.
You can change values to a pointer:
#include <stdio.h>
int main(void) {
int *values;
int a = 10;
values = &a;
printf ("the value is = %i.\n\n", *values);
return 0;
}
or you can store a into values[0]:
#include <stdio.h>
int main(void) {
int values[10];
int a = 10;
values[0] = a;
printf ("the value is = %i.\n\n", *values);
return 0;
}
think of arrays as parking lots:
You can store a car into a parking spot (array element)
You can write the lot number on a piece of paper (lot pointer, you can retrieve the car by giving that to the operator).
You cannot store a parking lot into another parking lot, they are not moveable.
Array designators are non-modifiable lvalues. You may not use an array designator in the left side of the assignment expression.
Thus the compiler issues an error for this statement
int values[10];
int a = 10;
values = &a;
^^^^^^^^^^
Related
This question already has answers here:
Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
(5 answers)
Closed 1 year ago.
#include <stdio.h>
#include <stdlib.h>
void main()
{
int i;
int *as[2];
for(i=0;i<2;i++)
{
*as[i]=i;
printf("%d\n",*as[i]);
}
}
This is my code. I am expecting to print values 1
2
But when I run the code the console prints nothing. What have I done wrong?
The * character in C language is not a simple decoration. It declares a pointer that has to point to something. Here you declared an array of two pointers, but never initialized the pointer themselves. And dereferencing an uninitialized pointer is explicitely Undefined Behaviour (the hell for C programmers). If you want to process integers, get rid of the pointers:
int as[2];
for(i=0;i<2;i++)
{
as[i]=i;
printf("%d\n",as[i]);
}
Or if you really want to process pointers, ensure that they are initialized to point to a valid object:
int *as[2];
int data[2]
for(i=0;i<2;i++)
{
as[i] = &(data[i]); // Ok, as[i] now points to a valid integer
*as[i]=i;
printf("%d\n",*as[i]);
}
BTW, the idiomatic way to get the address of an element of an array would be:
as[i] = data + i;
This question already has answers here:
Is an array name a pointer?
(8 answers)
Closed 8 years ago.
Suppose an array int a[10].
Why we can't do a=a+1 ? but the same is valid with a pointer variable.
int *ptr = a;
ptr = a+1;
How are both scenarios seen practically?
Because array locations are constant.
You can't change the value of a, since that represents the starting address of the array. Moving it doesn't make any sense.
With int *ptr; your variable ptr is just a single pointer and can of course be set to point to anywhere you like.
There's no contradiction here. It's a little like with functions, the name of a function evaluates to its address (called "a function pointer") but you can't assign to that either.
Because Array name is constant pointer pointing to its first element.You cannot change the value of constant variables,i,e what the const keyword is used for.
int a[10]; //here a (array variable is Const i,e you Cannot a=a+1)
int* const p=&a[0]; //here Also Same,Now p is Const i,e you Cannot p=p+1.
But Here:
int* pp=&a[0];//Here pp=pp+1 will work, Because pp is not Const.
You can if the array is a function argument
#include <stdio.h>
int rval(int a[10]) {
a = a + 1;
return a[0];
}
int main(){
int a[10] = {0,1,2,3,4,5,6,7,8,9};
printf ("%d\n", rval(a));
return 0;
}
Program output
1
I am trying to learn pointers in c.
As per my understanding int *p={1,2,3}; defines a pointer to the array of integers i.e {1,2,3}. So I deduct that p[0] is the first element of this array i.e 1. But my compiler(dev c++) is not giving any value and hanging for output.
How to find other elements of this array i.e 2 and 3 in terms of p? i.e is there any way to get these value through the pointer variable p. Please guide.
Sample program:
#include<stdio.h>
#include<conio.h>
main()
{
int *p={1,2,3};
printf("%d\n",p[0]);
getch();
}
Update: What about
int (*p)[3]={1,2,3};
int *p={1,2,3};
it is invalid C code. The right of = has to be a value of pointer type, not an initializer list of int elements.
Please enable all your compiler warnings and fix them.
int p[] = {1, 2, 3};
is valid C code. It initializes an array of 3 int elements.
#include <stdio.h>
#include <conio.h>
int main(){
int a[] = {1,2,3};
//int *p={1,2,3};//invalid
int *p1=(int[]){1,2,3};//valid in C99
printf("%d\n", p1[0]);//1
p1=a;
printf("%d\n", p1[0]);//1
//int (*p)[3]={1,2,3};//invalid
int (*p2)[3]=&(int[]){1,2,3};//valid in C99
printf("%d\n", (*p2)[0]);//1
p2 = &a;
printf("%d\n", (*p2)[0]);//1
getch();
return 0;
}
Yes,int (*p)[3] means pointer to array of 3 integers(My instructer also refers it as 2-dimensional pointer), in simple words it means that it can store address of the 1st row of a 2-Dimensional array,As you go on Increment the p it will point to the next subsequent row of the 2-D array, As demonstrated in below example.
int main(void)
{
int ary[2][3]={{1,2,3},{4,5,6}};
int (*p)[3]=ary;
printf("using ary: %d %d\n",p[0],p[1]); // outputs:2686708 2686720
printf("using ptr: %d %d\n",ary[0],ary[1]); //outputs:2686708 2686720
return 0;
}
And i think you are forgetting the basic property of pointers,They are used to store the address of other variables,You can't initialize pointers as you did in example,That is applicable only for strings,I feel you are trying to initialize integers pointers in the way of strings.
char *ptr="hello"; //correct
char *ptr[]={"hi","bye" } // correct
int *pttr=123; //Not Correct
int *pttr[]={{123},{456}} //Not Correct
This question already has answers here:
Why isn't the size of an array parameter the same as within main?
(13 answers)
Array length counting anomaly
(4 answers)
Closed 8 years ago.
I use a macro to get the number of the elements of an integer array, and I could get the right result of the number of the integer array in the main function, but I got the wrong answer if I use a getData function and send the pointer of the integer array as a parameter. I want to know why I got this wrong answer. Thank you!
the code of my program as follow:
#include <stdio.h>
#define LENGTHOFINTARRAY(intArray) ((int)(sizeof(intArray)/sizeof(int)))
int main (int argc, char *argv[])
{
int a[] = {5,8,9,4,11,7,15,25,1};
int getData(int *data);
printf("%d\n", LENGTHOFINTARRAY(a));
getData(a);
return 0;
}
int getData(int *data)
{
int i = 0;
for(i; i < LENGTHOFINTARRAY(data); i++)
{
printf("%d, %d\n", LENGTHOFINTARRAY(data), data[i]);
}
return 1;
}
and the result of my program is:
9
1, 5
I use gcc as my compiler.
The type of int* data is just int* and not int[9] like in main. The size of int* is is the size of any other pointer (4 or 8 bytes usually). There is no way to get the size of an array from the pointer.
And since arrays can't be passed by value in C (unless inside a struct or something) you have to pass the length of the array.
getData() sees data as an int*. It does not know how long it is.
You cannot determine the size of an array that is passed as a pointer to a function.
As you have defined
int a[] = {5,8,9,4,11,7,15,25,1};
int getData(int *data);
printf("%d\n", LENGTHOFINTARRAY(a));
getData(a);
so when you call "getData(a)" then it means you are passing the address of the very first element as &a[0];
so inside you function getData() as
int getData(int *data)
{
int i = 0;
for(i; i < LENGTHOFINTARRAY(data); i++)
{
printf("%d, %d\n", LENGTHOFINTARRAY(data), data[i]);
}
return 1;
}
the data is just a pointer to integer & it gets the pointer or address of the a[0];
so your macro now sees data as pointer to int. which causes the result as u have gotten.
void main()
{
int (*d)[10];
d[0] = 7;
d[1]=10;
printf("%d\n",*d);
}
It should print 10 but compiler is showing error such as follows:
test.c:4:7: error: incompatible types when assigning to type ‘int[10]’ from type ‘int’
Note that I have included some errors , not all.
As noted by chris, d is a pointer to an array. This means you use the variable improperly when you access it, but also that you will access random memory unless you assign d to point to a valid array.
Change your program as follows:
int main(void)
{
int (*d)[10]; /* A pointer to an array */
int a[10]; /* The actual array */
d = &a; /* Make `d` point to `a` */
/* Use the pointer dereference operator (unary prefix `*`)
to access the actual array `d` points to */
(*d)[0] = 7;
(*d)[1] = 10;
/* Double dereference is okay to access the first element of the
arrat `d` points to */
printf("%d\n", **d);
return 0;
}
In C, [] is the same as *, the pointer syntax. Thus the following lines are the same:
int** array2d1;
int* array2d2[];
int array2d3[][];
To relate to a closer example, the main function has the following popular forms:
int main(int argc, char** argv){ ... }
or
int main(int argc, char* argv[]){ ... }
Thus
int (*d)[10]
is the same as
int* d[10]
which is the same as
int** d;
int firstArray[10];
d = &firstArray;
Effectively, you are creating a pointer to a pointer (which is a pointer to an array) and allocating the first pointer to an array that 10 elements. Therefore, when you run the following lines:
d[0] = 7;
d[1] = 10;
You are assigning the 1st array's address to 7 and the second array's address to 10. So as Joachim has mentioned, to assign values, you need to deference twice:
(*d)[0] = 7
(*d)[1] = 10
Which says "Assign 7 to the 0th index at the value pointed by d". I hope that makes sense?
d is a pointer to an array of 10 ints.
int (*d)[10] is the declaration for a point to an array of 10 ints.
vs.
int *d[10], which is an array of 10 int pointers.
For more complex syntax like this (usually involving pointers), I use cdecl to help me decode it.
It's used in this form
int d[10]
I guess you are mistaken that d must be a "kind of pointer" and therfor you put an * before the d.
But that's not what you want. You wan to name an array of integer and the notation for that is seen above.
Concept of pointer can get confusing sometimes in C.
Consider an array int d[6] = {0,1,2,3,4,5}
Then, *d is equivalent to d[0]. d is itself an pointer to an array and *d dereferences that pointer and gives us the value.
Hence, following code would print the same values:
int main()
{
int (d)[10];
*d = 7;
*(d + 1)=10;
printf("%d\n",*d);
printf("%d\n",d[0]);
return 0;
}
result:
7
7
Please see http://codepad.org/LYY9ig1i.
If you change your code as follows:
#include<malloc.h>
int main()
{
int *d[10]; //not (*d)[10]
d[0] = (int *)malloc(sizeof(int *) * 10);
d[0][0] = 7;
printf("%d\n",d[0][0]);
return 0;
}
Hope this helps you!