Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C char pointer vs pointer to char array, increment dynamic allocation

Tags:

c

I am new to C, and I am having difficulty understanding the reason why the block of code below is not working.

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

int main()
{
    char *src = "http://localhost";

    /* THIS WORKS
       char scheme[10];
       char *dp = scheme;
     */

    //DOESN'T WORK
    char *dp = malloc(10);

    while (*src != ':') {
        *dp = *src;
        src++;
        dp++;
    }
    *dp = '\0';

    /* WORKS
       puts(scheme)
     */

    //DOESN'T WORK
    puts(dp);
}

The expected output is http to stdout. In both cases dp should be a pointer to an array of char pointers (char **). However it is printing nothing when using malloc method. I ran the code through GDB and my src and dp are getting erased 1 character at a time. If I enclose the while loop into a function call it works. I figured that the reason was because parameters are evaluated as a copy. However, then I read that arrays are the exception and passed as pointers. Now I am confused. I can work around this, but I am trying to understand why this way doesn't work.

like image 618
fatchance Avatar asked May 31 '11 21:05

fatchance


People also ask

What is the difference between char array and char pointer?

char[] is a character array whereas char* is a pointer reference. char[] is a specific section of memory in which we can do things like indexing, whereas char* is the pointer that points to the memory location.

Can you increment a char pointer in C?

Yes, you can apply the ++ increment operator to an object of type char , with the expected results in most cases: char c = 42; c++; printf("c = %d\n", c); // prints 43.

Can a char pointer be incremented?

For example, incrementing a char pointer will increase its value by one because the very next valid char address is one byte from the current location. Incrementing an int pointer will increase its value by four because the next valid integer address is four bytes from the current location.

How do you allocate memory to the array of char pointers?

In C, the RAM should use the following code to allocate the two-dimensional array: *contents = (char**) malloc(sizeof(char*) * numRows); **contents = (char*) malloc(sizeof(char) * numColumns * numRows); for(i = 0; i < numRows; i++) (*contents)[i] = ( (**contents) + (i * numColumns) );


2 Answers

You are changing dp inside the loop

dp = malloc(10);

let's say dp has the value 0x42000000

while () {
    dp++;
}

let's say the loop went 4 times, so dp has the value 0x42000004

*dp = 0;

now you put a null character at the address pointed to by dp

puts(dp);

and then you try to print that null :)

Save dp and print the saved value

dp = malloc(10);
saveddp = dp;
/* ... */
puts(saveddp);
free(saveddp); /* for completeness */

It works with scheme because scheme is an array and you cannot change that address!

like image 92
pmg Avatar answered Nov 15 '22 05:11

pmg


After the end of the loop, dp points to the end of the allocated string. You need to save dp right after the malloc and increment the copy, not the original pointer to the beginning.

like image 28
Blagovest Buyukliev Avatar answered Nov 15 '22 05:11

Blagovest Buyukliev