Question:
why this is invalid in C programming?(pointers)?
lilly
2013-08-17 03:44:56 UTC
For example, if p is a pointer to an integer, the following code is invalid:

p = 0;
*p = 12;

Thank u for your time :)
Five answers:
anonymous
2013-08-17 03:55:25 UTC
int *p;

p=0;

*p=12;



Since p is a pointer to int, "p=0" is setting the pointing address to 0 (why?).

Then with "*p=12", you're setting the value 12 on the address p (on 0).

Since that address isn't yours it will probably produce a segmentation fault.



Edit: A program is not assigned to use everything in RAM. There are other programs in RAM as well. If a program tries to edit another program's address it produces a seg error because you it's "illegal" to edit other programs' data. In theory, you could edit everything you want in RAM, but the problem is, OS doesn't allow it. You can only edit addresses that that are yours (what you have declared or allocated).



Also afaik, the address 0 is reserved for a 'null pointer'. So the objects that point to 0 are assumed to point to 'nothing'. You can think of it as a 'resting place' for pointers. Those pointers who don't want to point to anything just point to 0.



Thus even though there is an address 0, you're not supposed to edit it. It's OS protected.
anonymous
2013-08-17 16:39:28 UTC
You might need to access address 0 if you are writing an OS / a boot loader OR a DRIVER. But, p = 0 does not automatically mean that p will point to byte number zero.

The C standard dictates that p = 0 should "invalidate" the pointer if thats possible, that is to point it where its easy to catch the fact that its invalid. It is recommended that "dereferencing" (trying to access its target) a null pointer should halt the program, but that can be omitted if it could hurt performance. For example, the actual value might be an odd number, because a word or double word access will usually cause segmentation (or other) fault if tried for an odd value.
Daniel B
2013-08-17 11:42:21 UTC
This is happening because you are trying to use memory location 0. Yes, there is an addresss 0 in memory, but your application doesn't automatically get access to all the memory in the computer. Certain memory locations are protected from application use since they are used by other things, for example the OS. One of the causes of a segmentation fault is trying to access a memory location that you program doesn't have permission to access. If you need memory beyond what has automatically been assigned to your variables by the compiler you will need to use the malloc function to allocate more memory.
anonymous
2013-08-17 11:36:32 UTC
A null pointer usually means an error has occurred, and you are deliberately setting it to null.



Unless you are doing something esoteric, like writing an operating system, there is usually no need to set the value of a pointer explicitly. Usually they are assigned values in statements like.



int *p;

int a = 12;



p = &a;

-----------------------------------------------

Or:



p = malloc(512);





"isn't there will be any address with a '0'"



You don't know what is there; that's the point, and you can't go plonking things down wherever you like, without regard to what you might be overwriting. You might even be overwriting something the operating system put there for its own purposes.
Andy T
2013-08-17 11:20:01 UTC
The first line effectively knock out the position pointer is pointing at, get it? what is address 0 had this gone through whichever compiler that did not catch this?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...