0

I have string in main()

char *string = NULL;

Then I have a function

foo(char *s){
    s = realloc( ... );
    ..
}

what I want to do is reallocate memory for string if its not long enough, so when I access this function in main() do i have to put it like foo(&string) or foo(string)?

will it change address of s outside of foo() function?

lllook
  • 729
  • 2
  • 7
  • 20

4 Answers4

2

You will need to pass the address of the string pointer. Since your "string" is really a pointer to char that makes the thing you want a "pointer to pointer to char", which is written char **. When you pass it to foo() you need to pass the address of the pointer, so foo(&string) is correct.

When you reference the string in the function you you will need and "extra" dereference, for example:

int n = strlen(*s);

Which also applies to the reallocation:

*s = realloc(...);

Alternatively, you could pass it as usual and return the (possibly new) pointer as the value of the function. For example:

char * foo(char *s)
{
    if (...)
        s = realloc(...);
    ...
    return s;
}

Of course that means you have to call/use the function like so in main():

string = foo(string);
Dwayne Towell
  • 8,154
  • 4
  • 36
  • 49
  • but if I have this function foo() returning void, so it returns nothing, it really only modify that string s `foo(char *s) { if (...) s = realloc(...); ... }` is correct? – lllook Dec 13 '14 at 03:17
  • right, main will see the old value, which is no longer valid since you called `realloc()` on it – Dwayne Towell Dec 13 '14 at 03:20
2
  1. A string is not a pointer, an array or suchlike.

    A string in C is defined as a sequence of non-zero elements terminated by a zero element, unless otherwise noted of type char.
    Those strings are often saved in an array, and the array name is used to refer to them colloquially.

    Thus, you don't have a string in main().

  2. C is strictly pass-by-value, which means you cannot ever modify the expression used to initialize a function-argument from inside the function.

    But it also has pointers, thus you can pass the address of an object to enable modifying that object.

As an aside, don't use obsolescent implicit int return-type.

Putting that all together, you have:

#include <string.h> // strcpy
#include <stdlib.h> // realloc free abort
#include <stdio.h>  // puts

void foo(char** sz) {
    const static hw[] = "Hello world!";
    *sz = realloc(*sz, sizeof hw);
    if(!*sz) abort();
    strcpy(*sz, hw);
}

int main() {
    char* sz = 0; // Pointer for a string
    foo(&sz);
    puts(sz);
    free(sz); // Superfluous cleanup
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
-1

Have you check the realloc done in a main function.

Try this simple one.

main()
{
   char *string;
   string=(char*)malloc(10);
   printf("%p\n",string);
   string=(char*)realloc(string,20);
   printf("%p\n",string);
}

If you reallocate the memory using the realloc. If the needed memory is available in the memory after the already allocated memory realloc will not do the changing in memory. Just add the needed memory space to already allocated memory. If you are using the single variable most of the time memory address will not be changed.

If you want to get that using the function use this.

char*foo(char*s)              
{
 s=(char*)realloc(s,20);
 printf("foo:%p\n",s);
 return s;
}

Refer this.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
-1

char* str is a variable. which means it got an address in the memory, the content of this address(value of char* str) is an address to a char.

when you allocate memory, you are interested in char* str so you can use its contents, therefore, you send its address to foo, which make your code look like this:

    void foo(char** str) {
    *str = malloc(5*sizeof(char));
    *str = "1234";
}
int main() {
    char* str1 = NULL;
    foo(&str1);
    printf("str1 = %s\n", str1);
   return 0;
}

notice that it's a bad habit to allocate memory like that, because if foo was a function that allocates a memory for a string and returns the pointer of that string(char*), then you can't free this memory.

ThunderWiring
  • 738
  • 1
  • 7
  • 29