Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers, Arrays, Strings and Malloc in C

I'm currently learning about strings, pointers and arrays in C. I tried to write a program where an array holds three pointers to string addresses. It all seems to work but the program behaves strangely.

Here's the code:

char** getUserDetails()
    {

    char* host = "localhost";

    char* username = "root";

    char* password = "mypassword";

    // create array for holding pointers to strings

    char *userDetailsHolder[3];

    userDetailsHolder[0] = malloc(sizeof(char)*strlen(host));   
    strcpy(userDetailsHolder[0], host);

    userDetailsHolder[1] = malloc(sizeof(char)*strlen(username));
    strcpy(userDetailsHolder[1], username);

        userDetailsHolder[2] = malloc(sizeof(char)*strlen(password));
        strcpy(userDetailsHolder[2], password);

        return userDetailsHolder;
    }

    int main()
    {

    char** userDetails = getUserDetails();

    printf("Host: %s\nUsername: %s\nPassword: %s\n", userDetails[0], userDetails[1], userDetails[2]);

    printf("Host: %s\nUsername: %s\nPassword: %s\n", userDetails[0], userDetails[1], userDetails[2]);

            return 0;
    }

Output: The output indicates that something went terribly wrong

Host: localhost
Username: root
Password: mypassword
Host: root
Username: localhost
Password: Host: %s
Username: %s
Password: %s

The first printf seems to work, but the second has the wrong data in it. What did I do wrong?

like image 471
Frank Vilea Avatar asked Apr 08 '11 12:04

Frank Vilea


People also ask

Can you malloc an array in C?

In c programming, the array is used to store a range of values of the same data type and it occupies some space in memory which can be either static or dynamic. The malloc is a function used in the c programming for dynamic memory allocation.

How do you malloc an array of pointers?

Dynamically allocating an array of pointers follows the same rule as arrays of any type: type *p; p = malloc(m* sizeof *p); In this case type is float * so the code is: float **p; p = malloc(m * sizeof *p);

What is malloc() in C?

malloc() is a library function that allows C to allocate memory dynamically from the heap. The heap is an area of memory where something is stored. malloc() is part of stdlib. h and to be able to use it you need to use #include <stdlib. h> .

Can you malloc a string?

Allocating with malloc() does not initialize any string, only space waiting to be occupied.To add a null-terminating character, you either have to do this yourself, or use a function like scanf() , which adds this character for you.


2 Answers

The problem is that you are returning a pointer to an array which resides on the stack. userDetailsHolder is allocated on the stack and is not available after the function returns.

You could use malloc once more to allocate the array itself, and then it will be available after the function returns.

like image 124
Blagovest Buyukliev Avatar answered Oct 05 '22 14:10

Blagovest Buyukliev


Also, remember to allocate strlen(s)+1 bytes for strings. C strings are terminated with the zero byte and you need to make sure there's space for it.

like image 43
Kannan Goundan Avatar answered Oct 05 '22 15:10

Kannan Goundan