Reputation: 1452
I'm seriously confused. Upon read this and this
I decided to give it a try so I sat down a wrote the vanilla code that look like this
#include <stdio.h>
int main()
{
int *ptr;
printf("%p\n:",ptr);
*ptr = 65;
printf("PTR : \n \t VALUE: %d\n \t ADDRESS: %p\n",*ptr,ptr);
return 0;
}
And It worked.
0x7fff50ba0500
:PTR :
VALUE: 65
ADDRESS: 0x7fff50ba0500
Now, I have couple (3 to be precise) of question.
QUESTION 1:
I know that int *ptr
is not initialised in the above step. And one of the ways to do it is using.
int *ptr = malloc(sizeof(int));
*ptr = 65
or
int b = 65;
int *ptr;
ptr = &b; // or simply int *ptr = &b;
But unlike char pointer it cannot be directly assigned. Example in char pointer world
char *name = "apple";
or
char *name;
name = "viren";
Is a valid declaration but the following is not true for other example
int *ptr = 65; // not valid, since ptr should point to address.
Any explanation for this why?
QUESTION 2
As mention in the other StacK overflow answer
*ptr = 65;
is bad because ptr must point to a address. In above case it would point to 0x41
address (If by conversion to hex is correct) hence the code will result in segmentation fault
Question then why does the above code break and resulted in Segmentation Fault
.
QUESTION 3: I have a habit of not writing the command line argument declaration i.e the
i.e
int main(int argc,char *argv[])
After I successfully ran the code without the command line declaration the first time. (and it resulted in success much to my surprise). I added the command line argument declaration.
But, upon running the same code this time. Resulted in Segmentation Fault.
Question : So why does this difference that the code work without the command line argument but break with command line argument declaration.
Upvotes: 0
Views: 87
Reputation: 10184
Answer 1:
When you do char *name = "apple";
, you are specifying that name
is a character pointer that points to a string literal object "apple". In C, the string literals are stored in a special (read-only) section of the memory. So name
here points to that section of the memory where the literal "apple" is stored.
However, integer or float literals are usually part of machine instructions and are not stored in memory separately. So your statement int * p = 65
becomes invalid because the integer literal 65
is not separately stored in memory and hence there is no address that the int pointer can point to.
Answer 2:
Your understanding is not quite correct. The statement *ptr = 65
instructs that the int value of 65 should be stored at the address pointed to by ptr
. In your case, since you haven't initialized ptr
with a writable memory, it throws a segmentation fault.
Answer 3:
Specifying arguments to the main
function should not have anything to do with the segmentation fault that you are seeing. The behavior of assigning values to an uninitialized pointer is unpredictable. It may throw segmentation fault or it may not depending on whether the memory location it ends up writing to is writable or not.
Upvotes: 2