2
int main()  
{  
    int i=0;  
    int* p_numbers = ((int*) malloc (5*sizeof(int)));  
    int* temp;  
    temp = p_numbers;  
    for(i=0;i<5;i++)  
    {  
        *temp=i;   
        printf("temp %d p_numbers %d",*temp,*p_numbers);  
        temp++;           
    }
}

Please tell that the pointer assigned to temp i.e temp=p_numbers.

DOES temp not point to the same memory position where the p_numbers is pointing to?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
anshulkatta
  • 2,044
  • 22
  • 30

7 Answers7

5

The variables temp and p_numbers will point to the same memory location on the first iteration of the loop. Afterwards, temp is incrementing by an integer, but p_numbers won't.

Because of the assignments, p_numbers = [0,1,2,3,4], so you will print out:

temp 0 p_numbers 0
temp 1 p_numbers 0
temp 2 p_numbers 0
temp 3 p_numbers 0
temp 4 p_numbers 0

Think of pointers as pointing to a memory address and not more like the Java reference syntax.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
nmjohn
  • 1,432
  • 7
  • 16
2

temp point to same memory location as p_numbers. But the for loop will give different output. since you only increment the temp memory address. you need to increment p_number memory address as well.

temp++;            
p_numbers++; 

so that both point to same address location and give same output.

Kashif Khan
  • 2,615
  • 14
  • 14
1

Yes it does.

But when you print out output you only increment one of them so the other one still points to the first element.

See what happens when you increment both of them:

int main()  
{  
    int i=0;  
    int* p_numbers = ((int*) malloc (5*sizeof(int)));  
    int* temp;  
    temp = p_numbers;  
    for(i=0;i<5;i++)  
    {  
        *temp=i;   
        printf("temp %d p_numbers %d",*temp,*p_numbers);  
        temp++;           
        p_numbers++;    // see the result when you add this line
    }
}

output:

temp 0 p_numbers 0temp 1 p_numbers 1temp 2 p_numbers 2temp 3 p_numbers 3temp 4 p_numbers 4

Btw, not important but it's more efficient to pre increment than post increment (++i, ++temp, ++p_numbers) it does not need to make a copy of the variable before returning it.

stefanB
  • 77,323
  • 27
  • 116
  • 141
1

Temp points to same memory location as p_numbers point

answer to your code will be

temp 0 p_number 0

temp 1 p_number 0

temp 2 p_number 0

temp 3 p_number 0

temp 4 p_number 0

use p_number++ so that pointer p_number also increments.

Zlatan
  • 698
  • 7
  • 16
1
    int* p_numbers = ((int*) malloc (5*sizeof(int)));  

              +---+---+---+---+---+ 
p_numbers --> | x | x | x | x | x |
              +---+---+---+---+---+ 


    int* temp;  
    temp = p_numbers;  

p_numbers --+    +---+---+---+---+---+ 
            +--> | x | x | x | x | x |
temp--------+    +---+---+---+---+---+ 

you need also to free p_numbers since otherwise you get a memory leak.

also please make a habit of not casting the return value from malloc because in some cases this can cause hard to find errors

explanation:

the malloc is defined stdlib.h, if you forget to include that header, the malloc function will by default assumed to return int as that is the way in C for functions that have no prototype. Now if you have something like char*p = (char*)malloc(12); this may cause issues because you effectively casting the returned integer to a char*. By explicitly casting you silence the warnings from the compiler and if you have hardware/OS where sizeof(char*) != sizeof(int) you may get a hard to find error so just write

p_numbers = malloc(5*sizeof(int))

if you are using a C++ compiler, use new/delete instead.

AndersK
  • 35,813
  • 6
  • 60
  • 86
1

Both the pointers point to the same memory location because of this statement.

temp = p_numbers;

In the for loop, incrementing temp will make it point to the prior memory locations which makes it differ from p_numbers.

Manikandan
  • 53
  • 1
  • 7
0

Yes, you are setting the value of the pointer temp to the value of the pointer p_numbers.

Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57