tags:

views:

869

answers:

5

If I have an array of char pointers and another static array of characters, how do I assign each element of the array of pointers to each element of a static array?

(Trying to break down a problem into a smaller problem.)

Thanks.

   array of pointers                            array of char
    +----+                                           +----+                        
    |    |       ---->                               |    |
    | *  |                                           |  h |
    +----+                                           +----+
    | *  |                                           | i  |
    |    |         ---->                             |    |
    +----+                                           +----+
A: 

"If I have an array of char pointers"

char** arr;

"and another static array of characters"

char str[]="Hi there"

"how do I assign each element of the array of pointers to each element of a static array? "

len = strlen(str);
arr = (char **)malloc(sizeof(char *) * len);/*typecasting for C++ */
 if ( arr != NULL)
 {
   for(i=0; i < len; i++)
   {
     arr[i] = (char *)malloc(sizeof (char)); /* typecasting for C++ */
     if ( arr[i] != NULL)
       {
        arr[i] = &str[i];
        }
     else
       {
         /*If you choose to return from here,
          * free allocated memory upto now 
          */
         for(j=0;j<i;j++)
           {
             free(arr[j]);
           }
         free(arr);
         return 1;
       }

   }
 }

Hope it helps.

Aditya Sehgal
Thanks..........
Tommy
Thanks for bringing back *pointer* memories.
Aditya Sehgal
this is way too long and the way it frees memory is completely wrong. You're pulling stuff off the heap so it's got random garbage, which you are then trying to free?
Alex Gartrell
Way too long I agree. Can you explain why the freeing memory is completely wrong.
Aditya Sehgal
mallocing = asking for bytes to borrow. The memory manager gives you bytes from the heap. These bytes may have been used before, so they have random values (for the sake of argument). As you iterate through it, you're pulling sections of 4 bytes (all of which are random) and then trying to free that (keep in mind that space hasn't even been allocated). Instead, you should just free(arr);
Alex Gartrell
"arr = (char **)malloc(sizeof(char *) * len);"...isnt this allocating memory. And I am freeing only when one of the internal mallocs fail. So, if the malloc failed at i=3, then i free previously allocated 0,1,2.
Aditya Sehgal
+1  A: 

Aditya's re-submitted your answer. I'm not sure why you alloc'd all the pointers. "If I have an array of char pointers"

char** arr;

"and another static array of characters"

char str[]="Hi there";

"how do I assign each element of the array of pointers to each element of a static array? "

len = strlen(str);
arr = (char **) malloc(sizeof(char *) * len); /*typecasting for C++ */
if ( arr != NULL )
{
  int i=0;
  for(i=0; i < len; i++)
     arr[i]=&str[i];
} else {
   // error handling
}

Of course these pointers are valid only until static str array exists (normally until you exit the statement block). Very error prone, but this was precisely your request.

The OP wanted "to assign each element of the array of pointers to each element of a static array? ".....isnt each element of the static array a char instead of a char *. That is the reason I malloc'ed inside the loop for each element.
Aditya Sehgal
A: 
char **getPointers(char *initArr) {
char **arr;
int len, i;

if(initArr == NULL) return NULL;

/* + 1 for the null terminator */
len = strlen(initArr) + 1;

arr = (char **) malloc(sizeof(char *) * len);

/* since initArr is an array, we know the characters are contiguous in memory,
    so let's trust pointer arithmetic instead of dereferencing references we 
    create */
for(i = 0; i < len; i++) arr[i] = initArr + i;

return initArr;
}
Alex Gartrell
"how do I assign each element of the array of pointers to each element of a static array? ".....isnt each element of the static array a char instead of a char *.
Aditya Sehgal
+1  A: 
char  chrArr[] = "asd";
char* ptrArr[strlen(chrArr)];

char*  chrPtr = chrArr;
char** ptrPtr = ptrArr;

while (*chrPtr)
    *ptrPtr++ = chrPtr++;
Tomi Kyöstilä
+2  A: 

I am improvising Tomi's answer here.
The others are too long for such a question.

char  chrArr[] = "asd";
char* ptrArr[sizeof(chrArr)];
int i;

// A simple assignment loop over 'i' pointers and chars
for (i=0; i< sizeof(chrArr); i++)
    ptrArr[i] = &chrArr[i];
    ---------   ----------
//  pointer =   address of character

Since I used sizeof you get a 4th pointer here that is pointing to the NULL termination character of the string.

nik
Not bad, but you can't create a variable-sized array in C - change the definition of ptrArr to: char* ptrArr[3];
Dave Rigby
You are right, i got mixed up with strlen and sizeof... in fact, i read it as sizeof since that will work (this is the kind of error I have often seen people make in code reviews). Fixing it now
nik
Or you could malloc it, if he's trying to use this in a data structure.
Hooked
@Hooked, I think the real answer is the assignment on the last line. Other things are just for the context... that's how I made the first error.
nik