-5

I would like to understand why I needed to use malloc in this. The goal of my code was to separate "]" and ")" from ";". So like this "];" into "]" ";" and ");" into ")" ";". ptr is being used as an array of strings. I can't remember the technical name of array of strings. This works perfectly, but it save me lots of time to understand why this happened in the future.

char  *ptr[buflen];
for(x = 0; x < n; x++)
{
    printf("ptr[x] is %s \n", ptr[x]);
    cmp_str3 = strcmp(ptr[x], "];");
    cmp_str4 = strcmp(ptr[x], ");");
    if(cmp_str3 == 0)
    {
        printf("Match1 \n");
        strcpy(str1, ptr[x]);
        printf("Match2 \n");
        ptr[x][1] = '\0';
        printf("Match3 \n");
        //printf("ptr[x+1] %c %d \n", ptr[x+1], ptr[x+1]);
        //printf("ptr[x+1][0] %c %d \n", ptr[x+1][0], ptr[x+1][0]);
        ptr[x+1] = malloc(strlen("foo") + 1);
        ptr[x+1][0] = str1[1];
        printf("Match4 \n");
        ptr[x+1][1] = '\0';
        printf("Match5 \n");
        n++;
    }
    if(cmp_str4 == 0)
    {
    }
}
cmp_str3 = 0;
cmp_str4 = 0;
memset(str1, 0, 15);
memset(str2, 0, 15);
rocker
  • 1
  • 3
  • 1
    So you are saying that you wrote this code, that you have since then forgotten why you needed to use malloc, and you are now asking to figure out what you were thinking back when you wrote that? – void_ptr Nov 12 '15 at 21:21
  • `ptr` is an array of pointers. Attempts to dereference an uninitialized pointer leads to undefined behavior. – Mahesh Nov 12 '15 at 21:23
  • This is why you should **comment** your code, so you know your intentions *lendemain*. – Weather Vane Nov 12 '15 at 21:29
  • 1
    Oh........... just no. – Martin James Nov 12 '15 at 21:34
  • In this case, it would help if you *removed* those two commented lines, and all of the other superfluous `printfs`. Then you might actually see what your code is doing. And `malloc(strlen("foo")+1);` is an absurd way to write `malloc(4)` especially when you only need `malloc(2)`. – user3386109 Nov 12 '15 at 21:37
  • @void_ptr No. I'm saying I used google and figured it how to get it to work. I would now like to understand why it worked. http://stackoverflow.com/questions/1088622/how-do-i-create-an-array-of-strings-in-c – rocker Nov 12 '15 at 22:56
  • @WeatherVane I know exactly what I was doing. I used google to figure it out. I just would to understand the ***why***. http://stackoverflow.com/questions/1088622/how-do-i-create-an-array-of-strings-in-c – rocker Nov 12 '15 at 22:57
  • @user3386109 The printf's is how I realized what my problem was. I still don't understand the why part. http://stackoverflow.com/questions/1088622/how-do-i-create-an-array-of-strings-in-c – rocker Nov 12 '15 at 22:58

1 Answers1

0

The declaration

char *ptr[buflen];

creates an array of pointers. If ptr is a local variable (inside of a function) then the pointers are initially garbage values, they don't point to valid memory. If ptr is global or static, then the pointers are initially NULL.

In any case, before you can use one of the pointers to point to a string, the string needs to be in memory somewhere.

Given the line

char *ptr[3] = {"foo", "bar", "bletch"};

the compiler takes care of allocating memory for the strings, and then puts pointers to the strings into the array.

But if the strings aren't constant, then you have to allocate memory for the string yourself. That's what malloc does. It allocates the number of bytes that you request, and returns a pointer to that memory. You can save that pointer in the array, and then use strcpy or the other string functions to put the string into that memory.

Or you can copy characters one at time, which is what your code does:

ptr[x+1] = malloc(2);     // reserves two bytes of memory, and keeps the pointer 
                          // to that memory in the array of pointers
ptr[x+1][0] = str[1];     // copies a character into the first byte of the memory
ptr[x+1][1] = '\0';       // marks the second byte as the end of the string

So now you have a string in memory, and a pointer to that string in the array of pointers.

user3386109
  • 34,287
  • 7
  • 49
  • 68