array of pointers pointing the same variable - c

Can all the pointer members of an array point to the same variable?
ex: What happens when *a[i] is pointed to a variable C of value 5 and the length of the array is 3? Will a[0],a[1],a[2] have the values 5?

Yes, based on this answer
If all pointers are of the same type, there is no issue
int a = 10;
int *b[3];
b[0] = &a;
b[1] = &a;
b[2] = &a;
a = 5; // now b[0-2] are also equal 5.

No a[0],a[1],a[2] will not point to variable C, you have to explicitly make them point to variable C. Consider below example.
#include<stdio.h>
void main()
{
int *a[3];
int c=5;
a[0] = &c;
printf("%d %d %d", *a[0],*a[1],*a[2]);//this will print 5 junkvalue junkvalue
a[1] = &c;
a[2] = &c;
printf("%d %d %d", *a[0],*a[1],*a[2]);//this will print 5 5 5
}

Related

How does assigning an array of pointers to a void typecast pointer work?

I am trying to understand what happens in line #3.
What is the structure of pos. I know that removing ( void *) from line 3 makes it an assignment of type int* to int (*)[2] and arrays are not directly copyable. So what is typecasting doing here?
int a[] = {1,5,-3};
int * p = &a[0];
int (*pos)[2] = ( void *)p;
printf("%d \n",(pos[0][0])); //prints 1
printf("%d \n",(pos[0][1])); //prints 5
printf("%d \n",(pos[1][0])); //prints -3
printf("%d \n",(pos[1][1])); //prints garbage

point as array index

My background is C#, I have the following code(in C) that I need to tell what would be the output:
#include <stdio.h>
#define N 10
int main()
{
int a[N] = { 3,4 }; //<-- [3][4][0][0][0][0][0][0][0][0]
int *q = a; //<-- some address
char s[N] = "abcdefg";
int k = s[*a] - s[a[*q]]; //<-- what is that mean ?
char *p = s;
a[k] = k;
printf("%d %s\n",k,p+a[k]);
printf("%d %d %d %d %d",q[0],q[1],q[2],q[3],q[4]);
return 0;
}
Can someone please explain about line number 4?
I can't understand s[*a] is that mean s at index of *a? but *a is address not an integer. same question about s[a[*q]]
Considering
int k = s[*a] - s[a[*q]];
First *a gets the value at a[0] which is 3.
We know that q == a so *q is the same as *a which again is the value 3. The value at a[3] is 0.
So now we have s[3] - s[0] which is 'd' - 'a' which is 3 (assuming ASCII encoding), the value printed out for k.
Without the rest of the code, it is unclear if it will work at all. What is N, for example?
Specifically, s[*a]: a is the name of an array. *a is the same as a[0], because a is a pointer to the first element. So *a is 3, and s[*a] is s[3], which is the character 'd'

Printing a variable in C that was not assigned a value

I put this code into eclipse and run it
main()
{
int *p, *q, *r;
int a = 10, b = 25;
int c[4] = {6,12,18,24};
p = c;
printf("p = %d\n" ,p);
}
the output I get is
p = 2358752
what is this number supposed to represent? Is it the address of the variable?
If what i'm saying above is true would my answer to the following question be correct?
so lets say the following are stored at the following locations
address variables
5000 p
5004 q
5008 r
500C a
5010 b
5014 c[0]
5018 c[1]
501C c[2]
5020 c[3]
so would would the line
p = c;
be 5014?
int *p,
The above statement defines p to be a pointer to an integer.
In the below statement, c is implicitly converted to a pointer to the first element of the array a.
p = c;
// equivalent to
p = &c[0];
Therefore, p contains the address of the first element of the array. Also, the conversion specifier to print an address is %p.
printf("p = %p\n", (void *)p);
// prints the same address
printf("c = %p\n", (void *)c);
Yes, p is the address of c, which is the same as the address of c[0]. And yes, in your second example, p would be equal to 5014.

pointer to array in C : Address Arithmetic and Array Assignment

I have following pointer to array variable.
int (*p)[3];
int a[3] = { 1,2,3 } ;
int b[3] = { 11,22,33 } ;
int c[3] = {111,222,333} ;
I want to store these 3 array into variable p. How i have to allocate the memory for p
and How should i store these 3 array into p like array of pointer. Whether is this possible ...? and How..?
Note:
p = (int (*)[])malloc(3); Now this p is capable of pointing three
integer array which size 3 . How i have to assign these a,b,c to this
p ?
.
You don't need to allocate memory, it's allocated when you declare your array of pointers. Each pointer should point to the memory already allocated, but a, b, c are allocated automatically/statically, so you don't need to worry about that. Just assign them to the members of the array p and you're done.
If p is a pointer to array, then code should be:
int **p = malloc(sizeof(int*)*3);
...
p[0] = a; p[1] = b; p[2] = c;
...
free(p); /* when done*/
Declaring int *p[3] creates array of pointers, not pointer to array.
edit
If you want a pointer to an array then you can do this:
int a[3];
int *p = a;
And don't forget - you can use a on its own as a pointer to array it represents, where needed, you don't need a separate variable.
int* p[3];
int a[3] = { 1,2,3 } ;
int b[3] = { 11,22,33 } ;
int c[3] = { 111,222,333} ;
p[0] = a;
p[1] = b;
p[2] = c;
That's it.
Edit 1
A pointer to array ?
int** p;
int a[3] = { 1,2,3 } ;
//...
p = &a;
Edit 2
And a array of pointers to array of int :
int** p[3];
int a[3] = { 1,2,3 } ;
//...
p[0] = &a;
//...

Incrementing variables using pointers

I'm very new to dealing with pointers, and my C knowledge is fairly small. I'm trying to understand pointers. I wrote the following code to print a list of variables (a to f) like so:
0
1
2
3
4
5
I wrote the following code to do this:
#include <stdio.h>
int main(){
int a,b,c,d,e,f;
int *p;
int i;
a = b = c = d = f = 0;
p = &a;
for (i = 0; i < 5; i++){
*p += i;
printf("%d\n", *p);
p++;
}
return 0;
}
The idea was it works through the variables and increments each by an ever-increasing number (i). I am assuming that as you initialize the variables at the same time, they'd be placed next to each other in memory. However, I get the following output:
0
1
2
3
-1218283607
If I change the for loop to only go from 0 to 3 (i < 4), it works fine, printer 0 1 2 and 3. But when I wish to print the variable f as well, it doesn't seem to set it.
As I said, I'm very new to pointers so I've probably overlooked something silly, but I've been looking through my code over and over, trying to work it out.
Thanks in advance.
There is no guarantee that a, b, c, d, e and f will be adjacent in memory. If you want that sort of guarantee you need to use an array.
#include <stdio.h>
int main() {
int a[6];
int *p;
int i;
a[0] = a[1] = a[2] = a[3] = a[4] = a[5] = 0;
p = &a[0];
for (i = 0; i < 6; i++){
*p += i;
p++;
}
for(i = 0; i < 6; i++) {
printf("%d\n", a[i]);
}
return 0;
}
Here int a[6] is declaring an array named a that can hold six integers. These six integers can obtained via a[0], a[1], a[2], a[3], a[4] and a[5]. You are guaranteed that a[0], a[1], a[2], a[3], a[4] and a[5] are layed out contiguously in memory. Thus the line
p = &a[0];
sets p to the address of the first element. Each increment of this pointer moves us forward one position in the array.
The second for loop shows that first for loops correctly sets a[i] to i for i in {0, 1, 2, 3, 4, 5}. If you run this program you will see
0
1
2
3
4
5
on the console.
You forgot to initialize e. But yes, use a packed array.
It isn't safe to assume that stack variables are arranged in memory in any particular order.
You need to use an array, a struct or possibly a union to gurantee the ordering of your ints.
union {
int ary[6];
struct {
int a;
int b;
int c;
int d;
int e;
int f;
} s;
} u = {0};
p = &u.s.a;
for (i = 0; i < 5; i++){
*p += i;
printf("%d\n", *p);
p++;
}

Resources