simple problem w passing parameters

I have a question... im trying to figure out why this code does not work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void function(  char *c )
{
    c= (char *)malloc(25);
    strcpy( c, "If your reading this line than it passed" );
}

int main()
{
    char *c;
    
    function(c);
    printf( "output passed is: %s\n", c );
    free(c);
    system("PAUSE");
    return 0;
}





I thought it was due to the char* apparently being type cast(i not sure if thats type casting?) to malloc but apparently it is not. I looked up the malloc function in the reference documentation on her and I've found a nearly identical working example to what i have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* malloc example: string generator*/
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i,n;
  char * buffer;

  printf ("How long do you want the string? ");
  scanf ("%d", &i);

  buffer = (char*) malloc (i+1);
  if (buffer==NULL) exit (1);

  for (n=0; n<i; n++)
    buffer[n]=rand()%26+'a';
  buffer[i]='\0';

  printf ("Random string: %s\n",buffer);
  free (buffer);

  return 0;
}

The only majory difference in my code is that x is being passed as a function which is a different situation. I know how to fix it i just need to understand what is going wrong. this should be easy to you guys to figure out but can someone explain to me why the first mentioned code snippet does not work. by the way it works fine with the char* or the complete c=char* malloc(20); line commented out cause its simply strcpy going on. The question i may need to ask is what is going on at c= (char *)malloc(25);
I think you'll realize right away what's wrong with your syntax if I provide a correct example:
1
2
3
4
void function(char **c){
	*c=(char *)malloc(25);
	strcpy( *c, "If your reading this line than it passed" );
}
ew @ straight C.

Anyway, for starters, you're not mallocing enough. You only alloc 25, but the string you're copying is much larger. This will lead to heap corruption and some very nasty, hard to find runtime bugs. Always watch for buffer overflow!!!

As for your actual problem:

Take the pointers out of the situation for a second. Consider the following code:

1
2
3
4
5
6
7
8
9
10
11
12
void func(int local)
{
  local = 5;
}

int main()
{
  int passed = 0;
  func(passed);
  printf("%d",passed);
  return 0;
}


What this code does is pass the parameter by value. That is... 'local' is a separate variable whose contents get copied from 'passed'. However 'passed' is never changed throughout this process. So when func changes local to 5, 'passed' still is zero, and thus printf outputs "0".

This is exactly what you're doing. You're passing a pointer by value and expecting the function to change the pointer. It doesn't. See:

1
2
3
4
5
6
7
8
9
10
11
12
13
void func(char* local)
{
  local = malloc(100); // changes local -- NOT passed!
}

int main()
{
  char* passed;  // points to nothing/garbage
  func(passed); // passed is unchanged by this call
                     // so here, passed STILL points to nothing/garbage
  printf("%s",passed);  // bang, you're dead
  return 0;
}


The solution to this is to either:

1) return the pointer rather than pass it as a parameter

1
2
3
4
5
char* func()
{
  char* local = malloc(100);
  return local;  // okay
}


or 2) pass the pointer by reference (by pointer). IE: pointer to a pointer:

1
2
3
4
5
6
7
8
9
10
11
12
void func(char** local)
{
  *local = malloc(100);
}

int main()
{
  char* passed;
  func(&passed);  // this DOES changed 'passed'
  free(passed);  // don't forget to free it when done!
  return 0;
}



EDIT: blech -- helios is too quick ;_;
Last edited on
ok thanks guys. both solutions worked out. only you made a minor mistake helios on the strcpy the dereference operator cant be placed there. so the exact answer is
:
1
2
3
4
5
void function(char **c)
{
    *c=(char *)malloc(25);
     strcpy( c, "If your reading this line than it passed" );
}
only you made a minor mistake helios on the strcpy the dereference operator cant be placed there.
Yeah, you keep telling yourself that. Try compiling your "correction" and let me know how it goes.

The nerve.
i meant no offense i just wanted to post a correct version for someone who may look at this in the future. you only had a minor your version didnt work... no big deal.
Last edited on
but the version you posted is incorrect. helios' is correct.

Well actually they're both incorrect because neither of you are mallocing enough. But helios is closer to correct.
maybe theres more to it but helios version prints garbage and my version prints the correct output in dev c++.
That means there's a different bug in the caller that's making the bug in your version somehow work. My version is syntactically correct.

PS: I assumed OP had taken the trouble of counting characters. Apparently, I was wrong.
Topic archived. No new replies allowed.