ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
int main()
{
char *p;
printf("%p", p); //no address, prints (nil)
return 0;
}
my problem is when i declare a pointer in main function, it didn't initialize it with an anddress. In my friend's notebook (windows) make this initializing. My linux is linux mint 10. I cannot initialize it with an address or null. What can i do for initializing? Because i cannot use pointer variables.
It's not really a bug, in C all variables need to be initialised before used otherwise the results are going to be unpredictable. So you are saying that you want a pointer but you haven't said which area of memory the variable will point to. You may find that the variable has an address value but if that value is not within the permitted range of your program then the OS (to protect you, and the other programs that are running) will stop your program with a segfault.
According to the C standard that pointer is uninitialized, so it could be anything. In practice, that pointer is the first use of that location on the stack , so its value is null. You should not rely on the fact that the value is null.
Quote:
printf("%p", p); //no address, prints (nil)
I'm not sure why printf displays "nil" for a null pointer (a pointer to address zero).
I don't understand your problem. Is your problem the fact that "nil" is displayed?
Quote:
my problem is when i declare a pointer in main function, it didn't initialize it with an anddress.
You coded a non initialized pointer. It is accidentally initialized to zero. If you wanted it reliably and/or differently initialized, you should have coded it to be initialized.
Quote:
In my friend's notebook (windows) make this initializing.
I don't think that is correct. I think it is accidentally initialized to zero there as well.
Quote:
I cannot initialize it with an address or null. What can i do for initializing?
If you want an initialized pointer, code an initialized pointer, such as:
What I although find interesting is the fact, that p initializes with an address whereas a is as expected uninitialized in this:
Both p and a are uninitialized. That means their values are not reliably defined.
Their values are different when the program is compiled and run on my computers vs. what you and xndd report for your computers. The values might also be different if you added or changed code in distant parts of the program.
You and xndd also are saying "uninitialized" when the value happens to be zero and "initialized" when the value happens to be non zero. That is not the correct meaning of initialized or uninitialized.
"nil" for a pointer doesn't mean uninitialized. It means the value of the pointer (the address of the thing pointed to) is zero. Non zero also doesn't mean initialized. In xndd's code, neither variable is initialized. Whatever value is there (zero or non zero) is basically an accident.
Quote:
Originally Posted by hyperfluid
Does C initialize the adress for p consecutively to avoid a possible overlap with a?
I think that question as well as xndd's questions imply a basic misunderstanding of what a pointer is. I don't really know how to help correct that misunderstanding because to me pointers were always too simple to need explanation. But I'll try anyway.
The value of a pointer is the address of something else. When you use p in the code, that represent the value of the pointer (which ought to be the address of something). If you used &p in your code, that is the address of the pointer (where its value is stored), just as &a is the address where a's value is stored.
You showed this code:
Code:
printf("a: %p",a);
That takes the value of a (not the address of a) and prints it as if it were the address of something.
auto types also initialize but as you said with arbitrary values.
That phrase could be considered correct if there weren't established usage giving more precise meanings to words like "initialize".
Consider this code (at function scope):
Code:
int a;
int b=a;
b is initialized with an arbitrary value. a has the same arbitrary value, but it is not correct to say a was initialized with that arbitrary value. a has that arbitrary value as a result of not being initialized.
Okay, I see my problem. I was talking about initialization in case of memory allocation, so I created some confusion where originally the initialization of values was meant.
I was just wondering, why in my example p points to an address (e.g. for me 0x7fff113e37a0) while a points to (nil), whatever that means.
I was talking about initialization in case of memory allocation
So you are still confused about what a pointer is.
The C compiler takes no responsibility for making sure a pointer points to allocated memory. That is your responsibility when coding the program.
If an uninitialized pointer has a non zero value, that does not imply it points to allocated memory. If it happens to point to allocated memory, that is an accident.
Quote:
I was just wondering, why in my example p points to an address (e.g. for me 0x7fff113e37a0) while a points to (nil), whatever that means.
"nil" means address zero. 0x7fff113e37a0 is some non zero address. Both are accidents.
Uninitialized auto variables inherit whatever values happened to be left over from previous use of the stack. Even though these auto variables are at the start of main() previous use of the stack is possible, because part of the loader may run inside of the process being loaded and might use the same stack. Also some code that is part of your program (even if not part of your source code) runs before main().
I agree with hyperfluid. What i can't understand is why don't p points an address and prints "(nil)" while Windows points an address. Even if their values are null. Also if we define char *p,a; why p points an address while a not?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.