Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,423 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,977 people online right now. Registration is fast and FREE... Join Now!




pointers - Can someone help?

 
Reply to this topicStart new topic

pointers - Can someone help?, Segment error

HeatherLC
18 Jan, 2008 - 12:03 PM
Post #1

New D.I.C Head
*

Joined: 21 Oct, 2007
Posts: 9


My Contributions
Im trying to fix this segment, can anyone give me a tip as to how? Do i need to set the number as an adress?
Thanks


int *number;

printf ("%d\n", *number);


User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Pointers - Can Someone Help?
18 Jan, 2008 - 12:06 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
If that's all you're doing, then you'll have some problems. You need to assign that pointer 'number' to store the address of an int - otherwise you're dereferencing an uninitialized pointer. May work, may not; but either way it's probably not what you want to be doing and is generally bad practice.

You're probably looking for something more like:
CODE
int *pNumber;      //create pointer to an int
int number=10;     //create an int
pNumber=&number;   //assign pNumber the address of number using & operator

printf ("%d\n", *pNumber);


Hope that helps smile.gif
User is offlineProfile CardPM
+Quote Post

HeatherLC
RE: Pointers - Can Someone Help?
18 Jan, 2008 - 12:14 PM
Post #3

New D.I.C Head
*

Joined: 21 Oct, 2007
Posts: 9


My Contributions
- Find the error in each of the following program segments. If the error can be corrected, show how.
a)

int *number;

printf ("%d\n", *number);



Thanks for the help, but im still not sure how to make this segment correct? what does the p stand for? I know in class we have been using * as a pointer. Im really struggling in this course. any help would be helpful smile.gif
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Pointers - Can Someone Help?
18 Jan, 2008 - 12:15 PM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,550



Thanked: 67 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(HeatherLC @ 18 Jan, 2008 - 01:14 PM) *

what does the p stand for?

QUOTE(HeatherLC @ 18 Jan, 2008 - 01:14 PM) *

I know in class we have been using * as a pointer.


User is offlineProfile CardPM
+Quote Post

HeatherLC
RE: Pointers - Can Someone Help?
18 Jan, 2008 - 12:21 PM
Post #5

New D.I.C Head
*

Joined: 21 Oct, 2007
Posts: 9


My Contributions
I have been searching the text book all afternoon, but i really dont understand where to look. Not everyone is a programmer that comes to this site, and since i am a very very beginner, and this being my first computer programming course its really frustrating when people leave comments like you did.

So if anyone else has any advice on where to look for clarification on this question that would really be aprechiated. I am using the fifth edition C how to program


Thanks

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Pointers - Can Someone Help?
18 Jan, 2008 - 12:24 PM
Post #6

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Many people use a lowercase p prefix on variables that are pointers. It's not part of the syntax, it's just a convention. In this case, pNumber is a pointer to an int, that gets assigned the address of an int named 'number' (though it could have been any other int as well). It's the declaraction of the variable using the dereferencing operator is what defines it as a pointer; the p just helps to remember what type of variable it is.

It's looking as though this is either an assignment or a test, so I'm just going to remind you of the programming forum rules (see my sig, especially point 2) in case you missed them. We are happy to help, but definiitely unhappy about cheating.


* edit - oops. Also just noticed an error in my first post. Corrected the printf statement.
User is offlineProfile CardPM
+Quote Post

HeatherLC
RE: Pointers - Can Someone Help?
18 Jan, 2008 - 12:26 PM
Post #7

New D.I.C Head
*

Joined: 21 Oct, 2007
Posts: 9


My Contributions
Thanks for your help, but no this is not an assignment or a test, it is just a question the professor gave us in our lab today to work on to help us understand this chaper. But thank you for ur guildance.
User is offlineProfile CardPM
+Quote Post

musya
RE: Pointers - Can Someone Help?
18 Jan, 2008 - 01:50 PM
Post #8

D.I.C Regular
Group Icon

Joined: 25 Apr, 2007
Posts: 291



Thanked: 1 times
Dream Kudos: 50
My Contributions
its a correct way of printing i believe (im not a big c guy) but in order to see a value come out when you print the pointer to the screen you need to assign the pointer to a value, in this case an int value. some compilers will give you an error on the pointer if its not assigned a value, some wont, Microsoft allows alot of things that it probably shouldnt be allowing but some people like it because it frees up their mind from all the proper syntax/design. i.e using void main instead of int main, they do that so that you dont have to worry about typing return 0; at the end, not a big deal i would prefer to stay with proper c/c++ syntax, but back on subject you would need a value assigned to the pionter number in order to see a value when you print.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:31AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month