NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Pointer questions
May 24, 2014 by jmuthe |
I have a question about pointers. Here is a simple program:
On line 6 I write "x=a" so I would assume that when I print out x, it would equal a. However it doesn't. When I compile the program, I get the message
The output says that x=2686740 which is equal to a. However, I didn't define x to equal a. I defined *x to equal a. Why does this happen? |
---|---|
May 25, 2014 by jmuthe |
Sorry, I messed up on the first post. I wrote "On line 6 I write "x=a" so I would assume that when I print out x, it would equal a." I meant to write "On line 6 I write "int * x=a" so I would assume that when I print out * x, it would equal a." It seems that when I post text on this forum, I have to put a space between the asterisk and the x. Otherwise, the message leaves out the asterisk. I don't know why that happened. |
May 26, 2014 by Cameron |
I believe you are confusing the * symbol for declaring a pointer and the * symbol, the dereference operator. On line 6 when you write <<int *x=a;>> You are declaring a variable x that points to an integer. And you are equaling x to a (the reference value of t). It is equivalent to this:
So in the computers memory you have the following:
On line 8 <<printf("*x = %d klzzwxh:0003",*x);>> x is not a variable, x is the variable which happens to be a pointer. The * symbol is the dereference operator which gives you the value at the address where you pointer is pointing. That's why x = 8. X = 2686740 at that address the value stored is 8. So when you declare a pointer you use the * symbol. but after that the * symbol is the dereference operator. You can only use the dereference operator is the variable is a pointer. Hope that answers your question if not where a great website for resources. http://www.cplusplus.com/doc/tutorial/pointers/
|
May 27, 2014 by jmuthe |
I think I understand what you are saying but let me know if what I am saying is right. When I write "int *x=a;" I originally thought that I was assigning *x to the value a and int was the data type. However, that statement on line 6 is equivalent to this "int* x=a". They are the same statements. However, in the second statement it appears that I am writing that x equals a and x is the data type int*. If that is true, then I was merely confused with the spacing of the asterisk. It appears that the asterisk is part of the data type which turns "int" into "int*" instead of x into *x.. |
May 30, 2014 by Cameron |
Sorry, I'll try to better explain pointers this time around. It dosen't matter where you put the asterisk(*) symbol when declaring a pointer. So all of these are equivalent.
What you are doing in every case is declaring a variable X wich will point to a integer. (x) is not a variable x is the variable. In your first post you seem to think that x and x are two different varaibles, that's not true. There is only one variable x, it's just that on line 6 you're telling the compiler that x will point to an integer. So on line 8, when you use printf to display x. You are not actually telling the compiler to print (x). You are telling the compiler to print the value where x is pointing.X's value is equal to the address XXXXXXX.That's why on line 9 printf of x, the program gives you adress of t. So on line 8 you're telling the compiler to display the value pointed to by of X. That's why it gives you 25. Part of your confusion probably comes from the fact that the asterisk (*) symbol is used to do two different things. Declare pointers (line 6) and dereference them (line 8), telling the compiler x is a pointer and actualy printing the what x is pointing to. I hope this helps you.
As I said before you should propably read this article, good luck on understanding pointers!
http://www.cplusplus.com/doc/tutorial/pointers
Feel free to ask as many questions as needed.
Cam |
May 31, 2014 by jmuthe |
Okay, thank you. I think that I understand now. If I have any more pointer questions then I will be sure to ask. On a side note, I think that you had the same problem as me when you wanted to display the asterisk (*) symbol on this thread. I think that you wanted it to show but it didn't show. I realized that if you want to display it then you have to provide a backslash symbol before it. For example, if you write "*x" then the asterisk will not appear on this thread. Instead you have to write "\*x". |
Please log in to post a reply.
Did you know that NerdKits also has extra parts available for its customers? Learn more...
|