I have a confusion related to this program.
#include <stdio.h>
int main(void)
{
int value = 10;
char ch = 'A';
int* ptrValue = &value;
char* ptrCh = &ch;
int* ptrValue1 = value;
char* ptrCh1 = ch;
printf("Value of ptrValue = %d and value of ptrValue1 = %d\n", *ptrValue, ptrValue1);
printf("Value of ptrCh = %c and value of ptrCh1 = %c\n", *ptrCh, *ptrCh1);
}
I get two warnings while compiling this program
unipro@ubuguest:/SoftDev/ADSD/Module 1/Unit 1/Rd/C/System$ cc charPointers.c -o charPointers
charPointers.c: In function ‘main’: charPointers.c:11: warning: initialization makes pointer from integer without a cast charPointers.c:12: warning: initialization makes pointer from integer without a cast
And I know what they mean.
While running the program I get the following error.
unipro@ubuguest:/SoftDev/ADSD/Module 1/Unit 1/Rd/C/System$ ./charPointers
Value of ptrValue = 10 and value of ptrValue1 = 10 Segmentation fault
I know I am getting the error at second printf method.
So my question is if I store a value in pointer, why can开发者_JAVA百科't we de-reference it? Does it behave like a variable?
Thanks.
With the assignment
char* ptrCh1 = ch;
you will set the address to 0x41, not the value. The program will later try to de-reference 0x41 (ie. fetch the data at adress 0x41), which is an illegal address. Hence, the segmentation fault
A pointer store the address of something (or NULL). Thus an int* can store the address of an int. You get the address of something with the unary &
operator.
So, what does it mean to store 10
in an int pointer ? Well, 10 is not the address of an int, so thus you get the "initialization makes pointer from integer without a cast" warning, and since you are storing something else than the address of an int in an int* and try to dereference it, you get undefined behavior.
What's likely occuring in your case is 10 gets interpreted as a memory address, and when you dereference a pointer it goes out to fetch whatever is at that address (memory address 10 in your case). There's likely nothing there, that memory area is not mapped, and you segfault.
You can dereference it, but the chances that you get a valid chunk of memory are pretty slim. Valid here means, that your process has access rights to that spot.
The OS will prevent you from modifying memory that does not belong to your process. It responds to that kind of invalid memory access with a segmentation fault "exception".
A pointer points to a memory location (in which you can then store some value). Here
int* ptrValue1 = value;
char* ptrCh1 = ch;
you assign a pointer to the value of a variable (not to its address), so the pointer most likely points to an invalid memory location, thus the segfault.
Note that in many architectures, valid memory locations start at a high offset, so a small integer value like 10 or (the ASCII value of) 'A' can never point to a valid memory location. Such low memory locations are typically reserved/used by the OS itself (thus they are protected from modification - if you try, you get a segfault). The same happens usually if you try to directly access memory used by another process.
It's because of *ptrCh1
in the last line, you are trying to dereference it. Remove the *
to make it work (not that it makes much sense, though).
Sure that it must segfault. Your are storing whatever value in a pointer (and as you say the compiler warned you) and then you dereference it, regardless where this points to. What else do you expect?
精彩评论