Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-19-2004, 05:01 AM
|
#1
|
Member
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98
Rep:
|
Basic pointer scope Q
I´m having problems with pointers within pointer scope. In the following code. See following code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void another_func(char *ptr)
{
ptr = malloc(4);
*(ptr+3) ='\0';
strcpy(ptr, "Hej");
printf("\nptr: %s", ptr);
}
void some_func(char *ptr)
{
another_func(ptr);
printf("\nptr: %s", ptr);
}
int main (int argc, char **argv)
{
char *ptr;
some_func(ptr);
printf("\nptr: %s", ptr);
return(0);
}
The printout of this code is:
Why doesn't it print in the other 2 function scopes (function prototype should remain the same)?
Last edited by jnusa; 11-19-2004 at 05:20 AM.
|
|
|
11-19-2004, 05:18 AM
|
#2
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Rep: 
|
When you call 'another_func', the pointer 'ptr' from 'some_func' is passed by value. This means that the local variable 'ptr' is a copy of the value of the 'ptr' that was passed to it from 'some_func'. When you call 'malloc', you are reassigning the local version of 'ptr' to a new address, but since it is a copy, the 'ptr' in 'some_func'/'main' that was passed to it remains unchanged. Try changing the argument type of 'ptr' in your functions to 'char*&' instead of just 'char*'.
ta0kira
[EDIT]
Sorry, I was a little unclear. The addition of '&' makes the argument a reference to a pointer. This means that the value of the 'ptr' varable passed to the functions can be changed. It is just like having your argument type 'char**', except that you don't have to dereference it to change the value. Also, you need to use the 'free' function somewhere in there to deallocate the memory allocated by malloc.
ta0kira
Code:
void another_func(char **ptr)
{
*ptr = malloc(4); //<- reassigns the pointer passed to another_func
//...
}
// ----->
void another_func(char *&ptr)
{
ptr = malloc(4); //<- reassigns the variable passed to another_func
//...
}
Last edited by ta0kira; 11-19-2004 at 05:34 AM.
|
|
|
11-19-2004, 05:27 AM
|
#3
|
Member
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98
Original Poster
Rep:
|
Hmm... well I thougth that I was passeing the address of the pointer ptr, which was assigned, when i declared the var in main. If I try to change the function prototype to
Code:
void another_func(char *&ptr);
I get a compiler error ["syntax error before & token"]
Any other suggestions?
|
|
|
11-19-2004, 05:34 AM
|
#4
|
Member
Registered: Feb 2004
Location: Carpina, PE, Brazil
Distribution: Ubuntu 5.10
Posts: 44
Rep:
|
Declare:
Code:
void another_func(char **ptr)
Call:
Do this for both functions.
|
|
|
11-19-2004, 05:35 AM
|
#5
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Rep: 
|
That's right, I forgot it was just C...
ta0kira
|
|
|
11-19-2004, 06:42 AM
|
#6
|
Member
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98
Original Poster
Rep:
|
I can make the void some_func(char **ptr) work, and just dereference when in use. I still can't get the void another_func(char *&ptr) (I assume you mean '*&' within the function prototype right?) syntax to work. The compiler gives the same error. The *& seems to be the best solution, because you don't have to dereference the variable every time. Could you alter the eksisting code, with the '*&' example, so I can see prototypes and function calls. Thanks
[EDIT]
Didn't see last 2 post. Makes sense then. The '*&' is for C++ then?
/Jnusa
Last edited by jnusa; 11-19-2004 at 06:44 AM.
|
|
|
11-20-2004, 01:47 AM
|
#7
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Rep: 
|
Yes, that is C++. Sorry.
ta0kira
|
|
|
11-22-2004, 07:23 AM
|
#8
|
Member
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98
Original Poster
Rep:
|
Does the ** pointer notation not apply to structs (would think of a char as a struct too, I would think)? I've tried to do a similar test code, because I wanted to pass all my var's this way, but it doesn't seem to recognize my parameter as a pointer to my struct.
Test code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char *ptr;
}obj1;
void another_func(obj1 **ptr)
{
*ptr->ptr = malloc(4);
*(ptr->ptr+3) ='\0';
strcpy(*ptr->ptr, "Hey");
printf("\nptr: %s", *ptr->ptr);
}
void some_func(obj1 **ptr)
{
another_func(ptr);
printf("\nptr: %s", *ptr->ptr);
}
int main (int argc, char **argv)
{
obj1 obj;
obj1 *ptr;
ptr = &obj;
some_func(&ptr); //or '&obj' ?
printf("\nptr: %s", ptr->ptr);
return(0);
}
Am I doing this all wrong?
|
|
|
11-22-2004, 09:14 AM
|
#9
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Operator precedence has got you here:
your code:
Code:
void some_func(obj1 **ptr)
{
another_func(ptr);
printf("\nptr: %s", *ptr->ptr);
}
*ptr->ptr takes the member named ptr of the struct ptr and then dereferences it. What you need to do instead is:
Code:
printf("\nptr: %s", (*ptr)->ptr);
The same theory goes for another_func().
|
|
|
All times are GMT -5. The time now is 10:29 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|