LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 10-22-2007, 05:30 PM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
pass by reference in C


Why, in this example, I can't print the ptr value?

What am I doing wrong?

Code:
void call1(char* ptr);
void call2(char* ptr);

void teste()
{
  char* ptr;
  call1(ptr);
  printf("%s\n", ptr);

  call2(ptr);
  printf("%s\n", ptr);
}

void call1(char* ptr)
{
  ptr = "OLA";
}

void call2(char* ptr)
{
  ptr = (char*) malloc(strlen("HELLO"));
  strcpy(ptr,"HELLO");
}

int main()
{
  teste();
}
Thanks,
Pedro
 
Old 10-22-2007, 05:39 PM   #2
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
For me, the call2 method sould work, because I'm allocating memory, and put a value inside the memory. Even, if I do a malloc inside a function.

2 - Second questio. If I do a malloc inside a functin, when I return it, the value is deallocated?

Thanks,
 
Old 10-22-2007, 05:46 PM   #3
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
I think that the answer for my question 2 is yes. Here is the code:

Code:
  ptr = (char*) malloc(strlen("HELLO"));
  call3(ptr);
  printf("3-%s\n", ptr);


void call3(char* ptr)
{
  strcpy(ptr,"HELLO");
}
So, I do have a third question:

If when I call a function that would put a value in the parameter char*, and I don't know what value it will be. How should I do it?

For example:

Code:
void attrib(char* ptr)
{
// put a random value in ptr that I don't know what size will have
}


main()
{
char* ptr;
attrib(ptr);//How should I call this method? Like this?
return;
}
 
Old 10-22-2007, 05:49 PM   #4
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
Fourth question:

I the function accepts a char* var[] and a char** var2 parameters, how should I do it?
 
Old 10-22-2007, 07:32 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Why, in this example, I can't print the ptr value?
Ok well call1 is a really bad function and should cause a segmentation fault.
call2: you create a pointer in teste and pass to call2, the value of the pointer could be anything. Inside the function you then allocate memory and copy a string into it, yet when the function returns it will be just junk in the pointer. What is needed is to pass a pointer to pointer like so:
Code:
void teste()
{
  char* ptr;
  call2(&ptr);
  printf("%s\n", ptr);
}


void call2(char** ptr)
{
  *ptr =  malloc(strlen("HELLO"));
  strcpy(ptr,"HELLO");
}
Quote:
2 - Second questio. If I do a malloc inside a functin, when I return it, the value is deallocated?
No see above, but you do have a memory leak.

Quote:
If when I call a function that would put a value in the parameter char*, and I don't know what value it will be. How should I do it?
I don't think I fully understand this question, you have already shown you know how to get a string length.

Quote:
I the function accepts a char* var[] and a char** var2 parameters, how should I do it?
You will have seen this before many times in a main function.
int main ( int argc, char* argv[] )
or
int main ( int argc, char** argv )
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
questions on GCC-3.4.3 - Pass 2 and Binutils-2.15.94.0.2.2 - Pass 2 satimis Linux From Scratch 7 12-26-2005 09:23 PM
Planning to change root pass and oracle system pass sathyguy Red Hat 1 12-20-2005 09:53 PM
How do I pass a C++ object reference to GTK+ callbacks??? paulsm4 Programming 1 12-14-2005 03:32 PM
need some reference Hamid Moradmand Programming 0 05-17-2004 12:46 AM
function, pass by reference question true_atlantis Programming 5 04-02-2004 11:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:38 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration