-3

I'm going to take data structures course this year, so I decided to renew my knowledge about C by doing some simple tasks about pointers in C, and I have noticed one thing about passing pointers to functions, that I can't really understand.

Let's say we have a function:

void assignValueTen(int *var){
     *var=10;
}

We can call this function from main like this:

int main( void ){
     int x;
     assignValueTen(&x);
     printf("The value of x is %d.\n",x);
     return 0;
}

The output of this would be:

The value of x is 10.

We can also call this function like this:

int main( void ){
     int x, *y;
     y=&x;
     assignValueTen(y);
     printf("The value of x is %d.\n",x);
     return 0;
}

The output of this would also be:

The value of x is 10.

However, the statement below does not work as expected:

int main( void ){
     int *x;
     assignValueTen(x);
     printf("The value of x is %d.\n",*x);
     return 0;
}

Output of the code above is:

Process exited after 6.723 seconds with return value 3221225477

So, why does the code compile but not work as expected?
Is it because the pointer is not yet assigned to any address before in the last example?
Can somebody explain why this is happening in a little more detail?
Thanks a lot!

PlagueTR
  • 78
  • 2
  • 10
  • 1
    Where is `int *x;` is pointing? – kiran Biradar Oct 06 '18 at 21:48
  • figure out where the integer is defined, only a pointer is declared and there is no memory space for the variable itself. – Tom Kuschel Oct 06 '18 at 21:48
  • Turn on warnings, and learn what undefined behaviour is. – underscore_d Oct 06 '18 at 21:56
  • 'assignValueTen(x);' cannot modify x because parameters are copied into arguments. 'x' was not initialized before the call, it remains not initialized after the call and so the printf() fails when it tries to dereference x, (unless the 'assignValueTen call blows up first). – Martin James Oct 06 '18 at 22:02
  • @MartinJames The function can modify what the pointer refers to just fine, copying the pointer. The problem is that the pointer was never initialised, so dereferencing it is UB, i.e. in `assignValueTen()` already, not later. – underscore_d Oct 06 '18 at 22:04
  • @underscore_d yes - we do not disagree:) – Martin James Oct 07 '18 at 12:08

2 Answers2

1

The one that does not work is because x is not assigned any value in particular. The assignment in the function is assigning wherever that uninitialized variable happens to point and that is very unlikely to be a valid address (as the hardware sees things) and even if it is valid (again according to the hardware) it is not somewhere you should be writing to (because the address would 'belong' to something other than the code you are running in main).

You should only access addresses you have reason to know are valid (your assignValueTen function has such as an implicit requirement), C does not really have a way to enforce such requirement contracts though some other languages do.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • 2
    ...but it *does* have compilers that offer warnings, which trivially diagnose this misuse, and turning on warnings should be the first thing that anyone does. – underscore_d Oct 06 '18 at 22:06
-2

Because when you do:

int main( void ){
     int x;
     assignValueTen(&x);
     printf("The value of x is %d.\n",x);
     return 0;
}

or:

int main( void ){
     int x, *y;
     y=&x;
     assignValueTen(y);
     printf("The value of x is %d.\n",x);
     return 0;
}

you're always printing an int , while in the last example you're trying to print a pointer to an int, or at least that's the way i see it.

Stefano Raneri
  • 323
  • 4
  • 14
  • 1
    They dereference the pointer, so they are just as equally printing an `int` as all the other cases. The problem is that the dereference was invalid. – underscore_d Oct 06 '18 at 22:02