Home > OS >  add local variable and pointer variable
add local variable and pointer variable

Time:10-08

I'm a beginner in C language. I'm practicing several codes on my own, while doing I came across this algo. Below is the code.

#include <stdio.h>

int main() {
   
   int a = 1;
   printf("value of a = %d\n", a);
   printf("address of a = %u\n", &a);
   
   int *p;
   printf("value of p = %d\n", p);
   
   p = 2;
   printf("value of p = %d\n", p);
   
   a = a p;
   printf("value of addition =%d\n", a);
  
    return 0;
}



**OUTPUT** 
value of a = 1
address of a = 947268620
value of p = 947268880
value of p = 2
value of addition =6

Why I'm getting 6 instead of 3, is there anything I'm missing on result

CodePudding user response:

because you're not setting the value of the address p is pointing to to 2, you're assigning/pointing the variable int *p to the value 2 in memory, which is not memory you should be accessing. Instead, you need to point p to memory you have access to (a variable or dynamically allocated memory) and dereference the pointer using *p = 2 which accesses the value that p is pointing to. Your code should instead look like this

int a = 1;
printf("value of a = %d\n", a);
printf("address of a = %u\n", &a); 
int _p = 0;
int *p = &_p;
printf("value of p = %d\n", *p);
*p = 2;
printf("value of p = %d\n", *p);
a = a *p;
printf("value of addition =%d\n", a);

CodePudding user response:

One has to "walk right past" the compiler warnings about that code.

Below is the same code with coercive casting to silence some of the warnings. An additional calculation and print statement should make obvious how the compiler deals differently with the values of pointers (memory addresses) and the values of integers.

#include <stdio.h>

int main() {

    int a = 1;
    printf("value of a = %d\n", a);
    printf("address of a = %u\n", &a); // incorrect format spec for printing address

    int *p;
    printf("value of p = %d\n", p); // uninitialised and undefined behaviour

    p = (int*)2; // coercive casting integer to pointer-to-integer
    printf("value of p = %d\n", p); // incorrect format spec for a memory address

    a = (int)(a   p); // coercive casting address to integer
    printf("value of addition =%d\n", a);

    // ADDED these statements
    a = 1; // restore value of a
    a = a   (int)p; // coercive casting address to integer
    printf("value of addition =%d\n", a);

    return 0;
}
value of a = 1
address of a = 1703728
value of p = 1
value of p = 2
value of addition =6
value of addition =3    <== was this the expected result?

C will try to do its best with explicit program statements.
"Garbage in, garbage out."
Write correct code, not 'cute' code.

CodePudding user response:

Thanks for your response Awayy. Helpful. I got the output I needed. But the thing I need to know is, in what logic the line a = a p; gave 6 as result in my code.

As u said I'm assigning 2 as address to p(which is p = 0x2). So, when the addition happens a = 1 and p = 0x2 which has a random value that we don't know that is because I'm not assigning a particular address to the pointer variable p. It results 6 I'm I right.

  • Related