开发者

C pointer initialization and dereferencing, what's wrong here? [duplicate]

开发者 https://www.devze.com 2023-01-03 06:22 出处:网络
This question already has answers here: Why can't I directly assign an int to an int pointer like this: int *p = 6;开发者_如何学编程?
This question already has answers here: Why can't I directly assign an int to an int pointer like this: int *p = 6;开发者_如何学编程? (13 answers) Closed 5 years ago.

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.


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


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? :)


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.


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号