0
void change_it(int[]);

int main()
{
    int a[5],*p=1;

    void change_it(int[]);

    printf("p has the value %u \n",(int)p);

    change_it(a);

    p=a;

    printf("p has the value %u \n",(int)p);

    return 0;
}

void change_it(int[]) {

    int i=777, *q=&i;

    a = q; // a is assigned a different value
}
sg7
  • 6,108
  • 2
  • 32
  • 40

4 Answers4

1

For starters, when you initialize p, you're giving a pointer the value of 1, when it needs a memory location. NULL uses 0, but that doesn't mean you can -or should- just assign integer values to pointers.

Just as an fyi, you can cast the value of 1 like this:

int a[5], *p = (int *) 1;

There's like -2 reasons for doing this, though, the -1th reason being that the minimal type safety that C provides should be respected, and the -2th being that it makes the code hard to understand for other people.

I'm going to assume what you meant to do was not declare a pointer with an address value of 1 though, and say you meant to declare a pointer that holds a value of 1. Unless you have another variable that holds the value of 1 already, you're going to have to first dynamically allocate the pointer, then set its value.

int* p = malloc(sizeof(int));
*p = 1;

If you had another variable to use, you could instead create the pointer on the stack rather than dynamically allocating it, like this:

int* q;
q = p;

Now, calling the same print function on both would yield this:

printf("p has the value %d\n", *p);
printf("q has the value %d\n", *q);

Output: p has the value 1 q has the value 1

Addressing your main problem, you need to name the parameter in the change_it function, for example:

void change_it(int arr[])

Your program needs the parameter to be named, otherwise it has no idea of knowing you're trying to reference the array. The a variable you reference in the function is not bound to anything; the compiler will know be able to deduce what you're talking about.

Also, you don't need to redeclare the function prototype in your main function. The reason this is not a compiler error is that you can have as many declarations as you want, but only one definition. Again though, there's no reason to do this.

Another fyi, you don't have to name the parameters in your function prototypes, but it's good practice to both name them and be consistent with the names between the prototypes and the actual implementations so that people reading your code understand what's going on.

Also, you're using the %u specifier for the printf function, when you're not actually using unsigned decimal numbers. You're using signed decimals so you should use %d.

Lastly, your change_it function commits one crucial error preventing it from correctly changing the value of the passed-in array properly: you're setting the array that you passed in to the value of q.

Look at the function in your original code closely (pretend you named the input array a, as it looks like you mean to). You first declare an integer variable i and set its value to 777. Then, you create an integer-pointer variable q on the stack and correctly set its value to i. Note: You're not setting q to the value of i, but rather the address of i.

Why does this small but significant distinction matter? When you set a to q in the next line, you're changing the address of the array, specifically the first element of a five-element integer array, to point to the address of an integer variable. This is bad for a few reasons. First, the array is five integers long, but now it points to a single element. If and when you try to access elements 2-5, you'll get either meaningless garbage or a segmentation fault for trying to access memory you don't own. Even worse, the variable i is allocated on the stack, so when the function change_it exists, the function's data will be popped off the stack, and trying to access the address of i will yield either garbage or a segmentation fault for trying to access memory you don't own. See a pattern?

I'm not really sure how to correct this code, as I'm not sure what you were trying to accomplish, but correcting the aforementioned errors, your code now looks something like this:

#include <stdio.h>

void change_it(int arr[]);

int main()
{
    int a[5];
    int *p = a; // Equivalent to int *p = &a[0];

    printf("a address: %p\n", a); // Should be equal to p
    printf("p address: %p\n", p); // Should be equal to a

    a[0] = 1;        

    printf("a[0] = %d\n", a[0]); // 1
    printf("p has the value %d\n", *p); // 1

    change_it(a);

    p = a;

    printf("a address: %p\n", a);
    printf("p address: %p\n", p);
    printf("a[0] = %d\n", a[0]);
    printf("p has the value %d \n", *p);

    return 0;
}

void change_it(int arr[])
{
    int  i=777;
    arr[0] = i;

    // Could be just:
    // arr[0] = 777;
}

Output:

p address: 0x7fffc951e0b0                                                                                                                                                                   
a[0] = 1                                                                                                                                                                                    
p has the value 1                                                                                                                                                                           
a address: 0x7fffc951e0b0                                                                                                                                                                   
p address: 0x7fffc951e0b0                                                                                                                                                                   
a[0] = 777                                                                                                                                                                                  
p has the value 777 

Note: Your memory address can and probably will be different from these, all it matters is that p and a are equal in both.

Anyways, hope this helps. Let me know if you have any questions.

0

This part here:

int a[5],*p=1;
void  change_it(int[]); // Here, doesn't compile
printf("p has the value %u \n",(int)p);

That statement isn't just valid, as far as I know, you can't declare a function inside another function in C.

Also:

void change_it(int[]) // Here, an error
{
    int i = 777, *q = &i;
    a = q;
}

This function needs an argument, but you supplied only its type (being int[]),
void change_it(int a[]) fixes the problem

atmostmediocre
  • 198
  • 1
  • 9
0

Your program does not compile and produce warnings. It would not work as you intended.

1) p is a pointer. To access value which it points to you have to dereference it using * dereference opearator.

2)

void change_it(int[]); 

is not needed in the body of main.

3)

the invocation of change_it() seems to have no effect

If you want to change a[0] element inside the function change_it name the passing parameter to a and dereference the q pointer,

The working program may look as this:

#include <stdio.h>

void change_it(int a[]);

int main()
{
    int a[5] = {0}; // init all element of `a` to `0` 
    int *p;         // declare int pointer 
    p = a;          // p point to array `a`

    // print the first element of array `a`
    printf("a[0] has the value %d \n",(int)*p);

    // call function change_it, pass `a` as the argument
    change_it(a); 

    printf("a[0] has the value %d \n",(int)*p);

    return 0;
}

 // change the value of the first element of array `a` to 777
void change_it(int a[]) {

    int i=777, *q;   // declare int i and pointer
    q = &i;          // pointer `q` points to the `i` now 

    a[0] = *q;       // a[0] is assigned value = 777;
}

Output:

a[0] has the value 0                                                                                                                           
a[0] has the value 777
sg7
  • 6,108
  • 2
  • 32
  • 40
  • code has correction of above UB, but hard to imagine what is primary idea of Asker. This semantic was planned or not. Problem is in the question – Jacek Cz Apr 02 '18 at 14:49
  • @JacekCz Hi Jacek, thank you for your comment. You are right. We only know that `change_it()` has to have an effect. Since `a` was passed to `change_it()` we can assume that modification of element of `a` was planned. – sg7 Apr 02 '18 at 15:03
0

Alright, you I believe do not have basic understanding of a function: First lets start with declaration and definition:

void change_it(int[]); // THIS IS DECLARATION
int main ()
{
   void change_it(int[]); // THIS IS DECLARATION (duplicate and unnecessary 
   ....
}

void change_it(int[] a) // THIS IS DEFINITION 
{
   int  i=777, *q=&i;

   a = q; // a is assigned a different value
}

declaration of the function only needs (you can put parameter name for readability) a parameter type, where as definition has to have name of the parameter because in definition parameters are local variables.

printf("p has the value %u \n",(int)p);

This will print the address of p not the value of p. So this should be

printf("p has the value %u \n", *p);

And finally we get to the body of a function. Where you are depending on somthing that have been locally assigned and putting it back into parameters

void change_it(int[] a)
{
   int  i=777, *q=&i;

   a = q; // a is assigned a different value
}

so q is pointer and you are assigning address of local variable i to it. Well what happens when your program exists the function? i might disappear thus loosing its values and its address, which is assigned to q which means q is loosing its variable and value, and which is assigned to a which might loos its variable because it is pointing to i in your function.