0

I have a question and want to receive your advices. Please help me. I am writing some codes with using pointer. My code bellow:

#include <stdio.h>
#include <stdlib.h>

void test_pointer (char *t);

int main (){

   char *t = "abcdef";
   printf("Before excuting func: %s\n", t);
   test_pointer(t);
   printf("After excuting func: %s\n", t);
   return 0;
}
void test_pointer (char *t) {
   printf("In function - before allocating: %s\n",t);
   t =(char *) malloc(10);
   t = "123456789";
   printf("In function - after allocating: %s\n",t);
}

In this main(), I declared a char pointer and let its pointed to a string "abcdf" (length 7). But in the test_pointer() function, I used malloc() for allocating a new memory partition for that char pointer and then assigned a new value. That means char pointer had to be updated after using this function. But in real, it's not. Why?

when I executed this code, what I got is: Before excuting func: abcdef In function - before allocating: abcdef In function - after allocating: 123456789 After excuting func: abcdef

Please help me figure out what happened. Thank you very much.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
Micheal XIV
  • 495
  • 1
  • 5
  • 8
  • 1
    Is that C or C++? They are different languages! – too honest for this site Mar 19 '16 at 03:41
  • 1
    You're not passing your pointers *by address* (i.e. pointer to pointer), you're passing them by value. – WhozCraig Mar 19 '16 at 03:46
  • 2
    I have no idea why this was downvoted. Link to a duplicate, sure (and I'm sure there are many). But the question itself fills every criteria for a good question: He describes the intent of the code, provides the actual compilable code, walks through that actual code describing what he thinks it is doing, and finally describes what is seen as the unexpected results. I can only wish more posters of questions went that deep when asking a question. Have an uptick. – WhozCraig Mar 19 '16 at 03:50

2 Answers2

1

There are a few problems here:

  1. If you want test_pointer to be able to change the value of t in the caller, then the caller has to pass the address of t, and test_pointer needs to do a pointer assignment. As it is now, t is passed by value, which means it's copied and the called function can not affect it.

  2. In test_pointer, the call to malloc serves no purpose. It's allocating 10 bytes of memory, the result is being stored to t, and then you're immediately assigning something else to t (the address of a string constant), without ever using the memory returned by malloc. So either eliminate the call to malloc, or if you really want to use the memory returned by malloc, you can use strcpy to copy the string to that memory (or you can do the whole thing with a single call to strdup).

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
0

Because Both (t) are the Local Variables and not the Global Variables. A global variable which is in scope in both functions in this program will retain its changing values and print as you wish..

In Your Called Fucntion the "t" gets a new memory allocation and left the previous refernce..

In the Main Function , the Local Variable T is pointing to the Constant Array..

Ragav
  • 325
  • 1
  • 2
  • 13