Jump to content
  • Advertisement
Sign in to follow this  
Sabonis

Question concerning pointers

This topic is 5031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Everyone! I am just wondering why there are pointers in c++. I am trying to understand the use behind them (for example, why would someone return a pointer to a value in memory instead of returning the value itself?) For example, //returns a pointer char *read_line() { char temp[1000]; cin.getline(temp, 1000); char *res = new char[strlen(temp) + 1]; strcpy(res, temp); return res; } //returns an array of chars??? char read_line() { char temp[1000]; cin.getline(temp, 1000); char res[strlen(temp) + 1]; srcpy(res, temp); return res; } What would be the difference between the two? I'm not even sure if the latter would work... Its just an idea I had. Can someone enlighten the subject? Thanks in advance guys!

Share this post


Link to post
Share on other sites
Advertisement
I'll let the experts answer your question on when to use pointers. I'm still struggling with them myself. But reading your post brings up another question..

Aren't all arrays implicitely converted to a pointer when they are passed? Isn't that the reason you should always pass an integer with the size of the array along with the array (assuming you need to know the size in the function you are passing it to)?

Share this post


Link to post
Share on other sites
Quote:
Original post by Sabonis
Hi Everyone! I am just wondering why there are pointers in c++. I am trying to understand the use behind them (for example, why would someone return a pointer to a value in memory instead of returning the value itself?)
What if you wanted to write a function that could operate on values in arbitrary memory locations? A pointer is a memory reference mechanism; it holds a memory address.

Your second function wouldn't work, as there is no valid conversion between char [] and char. C has a complex notion of arrays, and pointers (the address of the first element in the array) are the only efficient way to manipulate them across scopes. C++ actually has object types that encapsulate arrays and can be considered atomic (and returned from functions) such as std::vector.

Share this post


Link to post
Share on other sites
From my book "As soon as an array name occurs in an expression, there is an automatic type conversion to a pointer, and the pointer will point to the first element in the array."

Another small question:

while(true)
{
//do something
}

is this not an infinite loop? I see it in so many examples of code!

But getting back to the pointers question, I'm just not understanding the specific uses of pointers besides using variables to achieve similar purposes.(Can you not make a program with normal variables that does the exact same thing as another similar function that uses pointers?)

About dealing with scopes, could you not declare a global array or would that just seem too messy?

Share this post


Link to post
Share on other sites
it is an infinite loop.
that piece of code used to confuse me as well.
normally in loops like the while (true) there is a piece of code
that'll force a break out of the loop. either a function or break;

when you take a data structures class you'll understand all the reasons for pointers. for now, just be curious and poke around some more but except that they are necessary.

Share this post


Link to post
Share on other sites
Here is an expample.

Model myBigFatModel;
//Load up your data from files or whatever


Render(myBigFatModel);
//This function here is going to make a copy of your model and then send the copy to the Render function. Your model could be a few megabytes huge! Where as if you were passing by pointer, you would only pass the address of the model which takes only 4 bytes on 32-bit systems.

Render(*myBigFatModel);
//Only sends 4 bytes.

There are quite a few other uses of pointers but i dont have the time to explain it now. Just understand what a pointer is and how it works.
If you cant find a use for them now, dont use them. In time there will be situations where you will see the need to use them.

Share this post


Link to post
Share on other sites
Quote:
Original post by HughG
Aren't all arrays implicitely converted to a pointer when they are passed? Isn't that the reason you should always pass an integer with the size of the array along with the array (assuming you need to know the size in the function you are passing it to)?
Well, the answer is complicated. See, there's actually an array type of sorts in C, but it's not user-accessible. Base type, array type and array length together constitute the type of an array, but that information does not survive scope transition (passing the array to a function). Consequently, the name of the array alone serves as a shorthand for the address of the first element, which is the "implicit conversion to pointer" you speak of.

Also because all the rich type information of a C array does not survive scope transition, it is necessary to either pass the length of the array or find some means of indicating the array termination, which results in null-terminated character arrays as the C representation of a string. Finally, this is the reason why sizeof(arr)/sizeof(arr[0]) does not yield the array length outside of the scope in which it was declared.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sabonis
From my book "As soon as an array name occurs in an expression, there is an automatic type conversion to a pointer, and the pointer will point to the first element in the array."
Many instructional books, sadly, are over-genericized. This is because these concepts are not simple, and can not be taught in a linear progression from zero to hero. They require a significant amount of retreading to fully grasp. In short, take what most books tell you as a "good enough" explanation.

Quote:
But getting back to the pointers question, I'm just not understanding the specific uses of pointers besides using variables to achieve similar purposes.
Consider the following code:
char * func(char * p, char * q)
{
while(*p++ = *q++)
;
return p;
}
Why does it work? What does it do? How do you propose doing the same without pointers? [All left as an exercise for the readers. Function and variable names stripped of semantic commentary. I'm off to bed!]

Share this post


Link to post
Share on other sites
Hmmm That makes a lot more sense now.. Ok thanks again! Feel free to comment more if you wish.... The more the info the better!

Share this post


Link to post
Share on other sites
Quote:
char * func(char * p, char * q)
{
while(*p++ = *q++)
;
return p;
}
Why does it work? What does it do? How do you propose doing the same without pointers? [All left as an exercise for the readers. Function and variable names stripped of semantic commentary. I'm off to bed!]


Hmmm... A char is an 8 byte value so I am not sure what will happen whenever p or q reaches 255. Does it wrap back around to 0 when it reaches 255? If so then this loop would go on forever...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!