views:

112

answers:

4

This should be super simple, but I'm not sure why the compiler is complaining here.

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

int main(int argc, char *argv[])
{
  int *n = 5;

  printf ("n: %d", *n);

  exit(0);
}

Getting the following complaints:

foo.c: In function ‘main’:
foo.c:6: warning: initialization makes pointer from integer without a cast

I just want to print the value that the pointer n references. I'm dereferencing it in the printf() statement and I get a segmentation fault. Compiling this with gcc -o foo foo.c.

+2  A: 

Because you're setting the value of a pointer to '5' (as in memory address 5).

You probably meant:

int *n = new int(5);

int *n = malloc(sizeof(int));
*n = 5;

malloc() returns a memory address, which is what you want to set your pointer to.

Brendan Long
Why was this voted down? I'm so confused.
Brendan Long
I'm so confused about when ANSI C added `new`.
Matthew Flaschen
Oops. I missed that.
Brendan Long
+6  A: 

You set the pointer to memory address 5, so that it points to whatever at address 5 might be. You probably wanted to make it point to an address where the value 5 is stored. For example:

int v = 5;    // Store the value 5 in a normal variable
int *n = &v;  // Make n contain the address of v, so that it points to the 
              // contents of v
sth
It is worth keeping in mind that assigning an integer to a pointer is implementation defined. In many cases, integers and pointers can be safely converted back and forth (provided the integer type is large enough to contain a pointer), but it is not a requirement of the standard that all implementations behave this way.
dreamlax
+2  A: 

you're setting n to be virtual memory address 5, because this memory isnt valid for your app you'll get a crash.

What you need to do is get some valid memory first, you've got two options - use the stack, or use the heap.

if you do something like the below you'll 'point' n at a stack variable, where you can then assign the value 5

int stackVar = 0;
int *n = &stackVar;
*n = 5;

the difference between the stack and heap is where the memory comes from - stack variables only live for the life of the function call. when the function returns the stack variables are "popped" and those pieces of memory are no longer valid. -- so from my example above, you could only use 'n' for as long as 'stackVar' is around, in this case until the point where you return from your function.

if you were to set

int *n = (int *)malloc(sizeof(int));
*n = 5;

you'll be creating a 'heap' variable that is sizeof(int) bytes large. this variable will last until you call 'free'

free(n);

keep in mind that doing this is somewhat heavy as extra bookkeeping memory is created in the call to 'new'. it wont be much, but it will be there. Usually you'll allocate larger classes/structs from the heap. one extra burden of using the heap is that you've got to remember to delete, else you'll 'leak'. I'm sure you've seen these before :-) the longer you run the app, the more memory it uses

does this make sense? :)

stuck
good call on the C/C++, my bad fixed
stuck
A: 

You need to set the pointer to the address of a value...

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

int main(int argc, char *argv[])
{
  int value = 5; 
  int *n = &value;

  printf ("n: %d", *n);

  exit(0);
}
semaj