LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-18-2010, 09:34 AM   #1
xndd
LQ Newbie
 
Registered: Jul 2010
Posts: 13

Rep: Reputation: 0
simple pointer problem in C


First of all here is the code:

#include <stdio.h>

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.
 
Old 12-18-2010, 09:49 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
well you need something to point it at:
maybe?
Code:
char b = 'a';
p = &b;
 
Old 12-18-2010, 09:54 AM   #3
xndd
LQ Newbie
 
Registered: Jul 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Yes it works. Also this works too:

#include <stdio.h>

int main()
{
char *p,a;
printf("c: %p\n",p); //prints an address
return 0;
}

Isn't that silly? Is this a bug? If so, what can i do?
 
Old 12-18-2010, 10:02 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 12-18-2010, 10:06 AM   #5
xndd
LQ Newbie
 
Registered: Jul 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Maybe i am wrong, i mean memory address initializing. Not the value.

Also why windows make address initializing?

Last edited by xndd; 12-18-2010 at 10:08 AM.
 
Old 12-18-2010, 10:10 AM   #6
xndd
LQ Newbie
 
Registered: Jul 2010
Posts: 13

Original Poster
Rep: Reputation: 0
This is silly because;

1:
Code:
#include <stdio.h>

int main()
{
char *p;
printf("p: %p\n",p); //no address
return 0;
}
2:
Code:
#include <stdio.h>

int main()
{
char *p,a;
printf("p: %p\n",p); //prints an address
return 0;
}
1 has no address, 2 has.
 
Old 12-18-2010, 10:17 AM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by xndd View Post
First of all here is the code:
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:
Code:
   char *p=0;
 
1 members found this post helpful.
Old 12-18-2010, 10:25 AM   #8
hyperfluid
Member
 
Registered: Aug 2010
Location: /ger/nrw/ac
Distribution: Ubuntu 12.04
Posts: 34

Rep: Reputation: Disabled
What I although find interesting, is the fact that p initializes with an address whereas a is as expected uninitialized in this:

Code:
#include <stdio.h>

int main()
{
char *p,a;
printf("p: %p\n",p); //prints an address
printf("a: %p",a); //prints (nil)
return 0;
}
Does C initialize the adress for p consecutively to avoid a possible overlap with a?

//EDIT: Also I couldn't find anything on that in the C ISO standard. Maybe s.o. has a reference for that

Last edited by hyperfluid; 12-18-2010 at 10:31 AM.
 
Old 12-18-2010, 10:32 AM   #9
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by hyperfluid View Post
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 View Post
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.

Last edited by johnsfine; 12-18-2010 at 10:45 AM.
 
2 members found this post helpful.
Old 12-18-2010, 10:39 AM   #10
hyperfluid
Member
 
Registered: Aug 2010
Location: /ger/nrw/ac
Distribution: Ubuntu 12.04
Posts: 34

Rep: Reputation: Disabled
But aren't just static pointers initialized with NULL when not specified at declaration?

//EDIT: Ah sorry, my mistake. auto types also initialize but as you said with arbitrary values. Sorry.

Last edited by hyperfluid; 12-18-2010 at 10:42 AM.
 
Old 12-18-2010, 10:52 AM   #11
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by hyperfluid View Post
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.
 
1 members found this post helpful.
Old 12-18-2010, 10:58 AM   #12
hyperfluid
Member
 
Registered: Aug 2010
Location: /ger/nrw/ac
Distribution: Ubuntu 12.04
Posts: 34

Rep: Reputation: Disabled
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.
 
Old 12-18-2010, 11:06 AM   #13
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by hyperfluid View Post
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().

Last edited by johnsfine; 12-18-2010 at 11:11 AM.
 
1 members found this post helpful.
Old 12-18-2010, 11:07 AM   #14
xndd
LQ Newbie
 
Registered: Jul 2010
Posts: 13

Original Poster
Rep: Reputation: 0
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?
 
Old 12-18-2010, 11:11 AM   #15
hyperfluid
Member
 
Registered: Aug 2010
Location: /ger/nrw/ac
Distribution: Ubuntu 12.04
Posts: 34

Rep: Reputation: Disabled
Quote:
Originally Posted by johnsfine View Post
The C compiler takes no responsibility for making sure a pointer points to allocated memory. That is your responsibility when coding the program.
That was what I wanted to know :-)
Thank you.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
a (simple?) function pointer problem mosca Programming 2 08-21-2008 11:02 PM
simple pointer question i.you Programming 7 04-14-2007 10:33 AM
Simple array/pointer question. smoothdogg00 Programming 3 03-15-2006 10:15 AM
a simple pointer problem kapsikum Programming 2 03-09-2005 11:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration