#include<stdio.h>
main()
{
    int *num2=20;
    printf("\n\n\n%d",num2);
}

When I run it as it is, it prints 20. If I use *num2 it causes segmentation fault. Why?

有帮助吗?

解决方案

Why printing num was okay but *num resulted in segmentation fault?

When you say, int *num2=20;. It is equivalent to

int *num; /* Type:(int*) num is a pointer to an integer and hence holds address of an integer object. */
num = 20; /* Type of the value assigned to num is (int).*/

You are assigning int to int * . You should have received a

warning: initialization makes pointer from integer without a cast[enabled by defaut]

num points to the address '20'. You have no idea even if that is a valid address.

Hence when you printed just num you had no problems. But dereferencing, the address(20) is invalid memory read , causes undefined behaviour and that resulted in segmentation fault.


Solution:

num should hold a valid address. Do not take this risk by assigning some random address yourself.

Avoid that risk by either creating an object of type int and assign num to hold the address of it or just allocate memory for that. Like,

i):

int var = 20;    /* var is a integer object holding the value 20 */

& unary operator helps you get the address of the operand. Use & with var to get its address and since var is an integer, store it in an integer pointer. In your case, num.

int *num = &var; /* num is a pointer to var. Holds the address of var. */

ii):

int *num = malloc(sizeof *num);
*num = 20; 

其他提示

int *num2 = 20;
printf("%d", *num2);

defines a pointer and initializes it with value 20. To initialize pointer means to assign an address it will point to to it, i.e. when you do *num2 (dereference the num2 pointer), you are trying to access int stored at address 20, which causes undefined behavior ~ which in this case turns into seg fault.

Try this instead:

int num = 20;
int *pNum = &num;
printf("%d", *pNum);

this defines a variable num and pointer pNum that points to variable num, when you dereference pNum, you are able to retrieve the value stored in num.

 int *num2=20;

It's not a pointer to an integer value. It's an invalid initialization.

This is a pointer to an int object whose value is 20:

int *p = &(int) {20};

To print the value of the pointer:

printf("%p\n", (void *) p);

As others pointed out you have a bug in your code. You confuse integer with pointers to integers You probably meant to do something like this:

int num2 = 20;
int* pnum2 = &num2;

Use %p to print value of a pointer:

printf("\n\n\n%p",pnum2);

Use %p formatting string:

printf("\n\n\n%p",num2);

That is not the correct approach. This is how you should go about it:

#include<stdio.h>
int main()
{
    int num1=20;
    int * num2 = &num1;
    printf("Number1=%d At Address=%p",*num2,num2);
}

You will get a seg fault on dereferencing int *num2=20; like this *num2. What this int *num2=20; means is that num2 points to a memory location 20 NOT the address of a location which holds the integer 20.

When you write "printf("%d", *num);" You are trying to obtain the value stored at the address 20(0x00000014 in hex). Value at address 20 is not accessible or could be junk. So it causes segmentation fault. Now see this code, this will produce the desired result 20 by dereferencing the pointer:

#include<stdio.h>
int main()
{
int *num2;
int value=20;
num2=&value;
printf("\n\n\n%d",*num2);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top