Hello guys.
Thanks for the help in the previous posts.
This one is regarding pointers in C language.
Does

1) c = *a and
2) *a = c

mean the same thing?
I think they are. Are they?

Thanks for the reply.

Recommended Answers

All 5 Replies

Hello guys.
Thanks for the help in the previous posts.
This one is regarding pointers in C language.
Does

1) c = *a and
2) *a = c

mean the same thing?
I think they are. Are they?

Thanks for the reply.

No they are opposite.

c = *a; //You are assigning the value that a points to *into* c
*a = c ; //you are assigning the value in c into what a points to

You always assign RHS to LHS, left to right.

Yes what Mr. Hollystyles said is right.

Btw i hope you dont mean to ask something like:

int* a = NULL ;
int* b = NULL ;

// some operation and allocating memory to ptrs.

// a and b point to the same mem. location 
// address in "a" overwritten by address in "b" 
a = b ;    


// a and b point to the same mem. location 
// address in "b" overwritten by address in "a" 
 b = a ;

Hope it helped, bye.

1) c = *a and
2) *a = c

I read it as "1) c is assigned the value pointed to by a", and "2) the thing pointed to by a is assigned the value of c".

Thx very much guys for the contribution. I do understand the difference now. Though I got a concern though:
Using this code:


#include<stdio.h>
main()
{
int *a, int *b;
}


It means a and b are pointing to the same memory address, right? But int is a type, not technically a variable ( and I suppose pointers only point to variables). Does it imply that a and b are pointing towards any variable with int as its type? Or does it just mean they are pointing to type int (acting as a variable)?

Thx very much for the replies.

When new pointers are created they point to a random location in memory, more like nowhere. It doesnt have to be necessarily an integer variable. Just keep in mind that you cant rely on the value of uninitialized or the pointers which point nowhere. Any attemp to access the value pointed by these variables will give you a junk value and any attempt to modidy that location which doesnt belong to you will give you a run time error.
So a better programming practice is to ground the pointer variables newly created like this:

int main (void)
{
    int* p = NULL ; // ground the pointer var
    int val = 10;

    p = &val; 
    // pointer now points to the "value" or has the addr of 
   // "val" as its value.

  return 0;
}

At the end of program execution:

eg.
Content of val = 10
Address of val = aabb0034
Content of p = aabb0034
Addr of p = some_arbitrary_value

Hope it helped, bye.

PS: main returns an int like mentioned by me above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.