LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
LinkBack Search this Thread
Old 11-19-2004, 05:01 AM   #1
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Rep: Reputation: 15
Basic pointer scope Q


Im 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:

Code:
Ptr: Hej
Ptr: 
Ptr:

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.
 
Old 11-19-2004, 05:18 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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.
 
Old 11-19-2004, 05:27 AM   #3
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
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?
 
Old 11-19-2004, 05:34 AM   #4
LauroMoura
Member
 
Registered: Feb 2004
Location: Carpina, PE, Brazil
Distribution: Ubuntu 5.10
Posts: 44

Rep: Reputation: 15
Declare:
Code:
void another_func(char **ptr)
Call:
Code:
another_func(&ptr)
Do this for both functions.
 
Old 11-19-2004, 05:35 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
That's right, I forgot it was just C...
ta0kira
 
Old 11-19-2004, 06:42 AM   #6
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
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.
 
Old 11-20-2004, 01:47 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Yes, that is C++. Sorry.
ta0kira
 
Old 11-22-2004, 07:23 AM   #8
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
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?
 
Old 11-22-2004, 09:14 AM   #9
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 56
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().
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
basic pointer prob. jnusa Programming 6 01-13-2005 02:42 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
scope suchi_s Programming 1 10-30-2004 07:07 AM
question about scope in c++ PTBmilo Programming 7 02-10-2003 04:59 PM


All times are GMT -5. The time now is 11:05 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration