basic question about pointers and local vars 
Author Message
 basic question about pointers and local vars

This is basic and I should know it but I don't.  Here's my query--
So I know that if you have:

main () {
char string[10];
function(string);
...

Quote:
}

function (char *string) {
   string[0] = 'a';
   string[1] = 'b';
   ....

Quote:
}

This changes what the string is in main because string is a pointer.  But
what is the
standard way to get a string returned by a function if you don't want to
allocate
space for the array in main.  For example, is this valid:

main() {
char *string;
function(string);
...

Quote:
}

function(char *string) {
   string = (char *)malloc(...);
   string[0] = 'a';
   string[1] = 'b';

Quote:
}

It doesn't seem like it should be because the value of the pointer itself
gets changed
here and I would have thought that that gets lost when control goes back
to main, but
it does work when I try it.  So where is the problem and what is the right
way to do this?
Thanks.

--Gene Cutler

  TheRoadToNoWhere
  http://www.*-*-*.com/



Tue, 04 Aug 1998 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. global vars vs: local vars

2. how to access local and global vars in a function

3. global vs local vars

4. Global vs. Local vars and .MAP

5. VC6 SP3 debugger, local vars not recognized

6. Initialization of a local pointer - a question

7. Basic pointer casting question

8. QueryInterface basic question-- why the double pointer?

9. QueryInterface basic question-- why the double pointer?

10. REALLY basic question : pointers

11. Global vars versus pointers

12. environmnet vars and C question

 

 
Powered by phpBB® Forum Software