Jump to content

Array and Pointer Assignments

pureGold

 

void f(int p[]);
int main() {
    int p[] = { 1, 2, 3, 4 };
    f(p);
    return 0;
};

void f(int p[]) {
    int* q;
    const int* r;
    int s[10];
    p = q; //1
    p = r; //2
    p = s; //3
    q = p; //4 
    q = r; //5 
    q = s; //6 
    r = p; //7 
    r = q; //8 
    r = s; //9 
    s = p; //10 
    s = q; //11 
    s = r; //12
}

 

 

How is 1 a legal assignment? If p is an array and it is assigned to a pointer, what does that do? There is no index to put the q pointer in the p array.

 Since 1 is a legal assignment, how come 2 and 4 would not be a legal assignment? Is it because r is a const int instead of just int type? 

 

Also why are 10, 11, and 12 illegal assignments? If p is able to be assigned to q, then why wouldn't s be able to be assigned to q? I assume because there is no index for where to put the assignment such as s[0], but 1 doesn't need one which is confusing me.

Link to comment
Share on other sites

Link to post
Share on other sites

Setting an array equal to a pointer is like shifting where the array is. The pointer is the first part in the RAM where an array will be (single int or an array of ints). The const int pointer doesn't work because it's formatted differently than a normal int. 

 

4 doesn't work because you can't assign an array value to a pointer of a different type. 

As for 10, 11, and 12, 10 doesn't work because p no longer equals the initial values that it was initialized to in main(). Or it could be that the arrays are of different sizes. I've never dealt with that before. I'm sorry.

 

Really, you should try running the assignments separately, making sure that you know what each value is before going into the assignment, and checking what the compiler spits out if you're having a problem. Each group of assignments before the next is going to change the expected result.

Link to comment
Share on other sites

Link to post
Share on other sites

First of all, please be a bit more specific about what we're reading, i mean, it is C, but i had to deduce that, it helps when shifting thoughts  between languages :I (im in a java class atm lol)

 

So 1, basically a pointer is an memory address, its just grabbing the array and telling it "now you are stored here", and therefore it is valid.

4 is a constant as you said, and you cant modify constants, so it is invalid, as p[] is not a constant and CAN be modified, therefore different types.

2 is invalid because an array is not a pointer, a pointer can point to an array, but not the other way round (You can have an array of pointers though).

int p[2];
int* a;
int* b;

p[0] = a;
p[1] = b;

10 is invalid, because C does not associate the array data as an assignment, you need to the values directly, as you correctly guessed.

int p[] = {1,2,3,4}
int a[sizeof(p)];

for (i=0;i<sizeof(p)+1;i++) 
{
a[i] = p[i];
}

11 and 12 are the same as those above :)

Planning on trying StarCitizen (Highly recommended)? STAR-NR5P-CJFR is my referal link 

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Xaring said:

4 is a constant as you said, and you cant modify constants, so it is invalid, as p[] is not a constant and CAN be modified, therefore different types.

You may want to look back at the code. The variables are pretty similar, but 4 has nothing to do with constants.

 

I did mean to add that it can point to any address that the array may have, but you can't assign the array value as an address to my original post, but oh well.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, mkylem said:

-snip-

Oh true, my bad, i swapped 2 and 4 in my mind :I Swap the answers around.

 

What we mean is basically that an "array int" is different from a "constant int", so they shouldn't be able to interchange.

 

Planning on trying StarCitizen (Highly recommended)? STAR-NR5P-CJFR is my referal link 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Xaring said:

Oh true, my bad, i swapped 2 and 4 in my mind :I Swap the answers around.

 

What we mean is basically that an "array int" is different from a "constant int", so they shouldn't be able to interchange.

 

Well, it's not that it's a constant int. It's that it's a pointer to a constant int and constants are managed in such a way to save RAM and may not operate the same as int pointers in RAM.

Link to comment
Share on other sites

Link to post
Share on other sites

#1 is a bit more complicated than people are making it out to be. While they can be used for similar things, arrays and pointers are not the same. You also can't pass an array to a function which makes the line 

void f(int p[])

not really accurate.

 

When you pass an array to that function it decomposes to a pointer making the type of p an int* instead of the int[] you expect.

For #1 you're assigning an int* to an int*, so that works fine. However for #10 and #11, s is type int[] while p and q are int* and you can't convert from int* to int[], only the other way.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×