please help me understand when does one use
*n=m

and
when to use

n=&m

int *n,m;
    *n=5;
    m=2;
    *n=m;

//gives an error

if n is a pointer i.e. int n*; and m is a simple integer i.e int m; then you use n=&m to store the memory address of m into n. And you use *n=m to store the value of m into n.
* is called the Value at Operator
& is called the Address of Operator

Maybe this will clear it up:

void main(void)
{ 
	int *p, i;         // integer i & uninitialized pointer to an integer *p
	i = 10;            // put a value in i
	p = &i;            // initialize the pointer *p to the address of i
	printf("%d\n",*p); // printf the value stored in i by using the address of i
                            //    stored in p  
}

Edited 5 Years Ago by Narue: Added code tags

the point is the code that I posted gives an error
why?

and somtimes the other exp gives an error...
is it compiler dependent? I used GNU gcc compiler and the code is in c(not c++)

the point is the code that I posted gives an error
why?

Because your code is wrong.

and somtimes the other exp gives an error...

Because your code is wrong.

is it compiler dependent?

No, your code is just wrong.

Read this and this.

This is how it works for me:

#include <stdio.h>
#include <stdlib.h>

int main(){
    int *n, m = 2;
    *n = 5;
    printf("Numbers *n: %i m: %i", *n, m);
    n = &m;
    printf("\nNumbers *n: %i m: %i", *n, m);
    return EXIT_SUCCESS;
}

Edited 5 Years Ago by pyTony: n/a

int *n, m = 2;
*n = 5;

n is an uninitialized pointer, yet you dereference it. The code is wrong, and "works for me" is not synonymous with "correct".

n is an uninitialized pointer, yet you dereference it. The code is wrong, and "works for me" is not synonymous with "correct".

Then I would initalize dummy variable:

#include <stdio.h>
#include <stdlib.h>

int main(){
    int dummy = 5, m = 2;
    int * n = &dummy;
    printf("Numbers *n: %i m: %i", *n, m);
    n = &m;
    printf("\nNumbers *n: %i m: %i", *n, m);
    m = 3;
    printf("\nNumbers *n: %i m: %i", *n, m);
    return EXIT_SUCCESS;
}

Edited 5 Years Ago by pyTony: n/a

So then how do I store the value of a variable in another variable that is pointed to by a pointer? in other words how do i change the value stored at *n to the value of m?
(and I dont want n to point to the same address as that of m... i.e. change in one should not effect the other)

I did go through the link eternallyconfuzzled ( an excellent tutorial on trees I must say that I had been following even before it (the link) was posted here) but could not find the answer and the "correct" way to do so.

// Not Code
// From my previous post:

int *p, i; // integer i & uninitialized pointer to an integer *p
i = 10; // put a value in i
p = &i; // initialize the pointer *p to the address of i
//
// You can now use *p to get the value stored in i i.e.
//
printf("%d\n",*p); // print the value in i using the address of i in *p

"p" will not change unless you change it. It will always contain the address of "i". Changing the contents of "i" has no effect on "p". You might use this pointer when looping through a table loading "i" with members of an array and the doing something with each member. "p" is always pointing to "i" but "i" changes as you load each member of the array into "i".

Edited 5 Years Ago by jnawrocki: n/a

This article has been dead for over six months. Start a new discussion instead.