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

## All 13 Replies

http://en.wikipedia.org/wiki/Pointer_(computing)#C_pointers

You must allocate space in memory for an integer.

``````int *n = NULL, m;
n = malloc(sizeof(int));
// also check if n isn't null here
*n = 5;
m = 2;
*n = m;
free(n);``````

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
}``````

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;
}``````
``````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;
}``````

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

@jnawrocki:

but I donot want p to change when I change i

@jnawrocki:

but I donot want p to change when I change i

Then why do you want a pointer?

"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".

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.