Question concerning pointers
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!
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)?
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)?
Quote:Original post by SabonisWhat 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.
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?)
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.
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?
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?
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.
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.
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.
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.
Quote:Original post by HughGWell, 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.
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)?
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.
Quote:Original post by SabonisMany 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.
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."
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!]
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!
Quote: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!]char * func(char * p, char * q){ while(*p++ = *q++) ; return p;}
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...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement