分配指向数组的指针数组

时间:2022-10-11 18:38:45

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?

如果我有一个char指针数组和另一个静态字符数组,如何将指针数组的每个元素分配给静态数组的每个元素?

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

(试图将问题分解为更小的问题。)

Thanks.

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

5 个解决方案

#1


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

我在这里即兴创作Tomi的答案。其他人对于这样的问题来说太长了。

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.

因为我使用了sizeof,所以你在这里得到第4个指针,它指向字符串的NULL终止字符。

#2


char  chrArr[] = "asd";
char* ptrArr[strlen(chrArr)];

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

while (*chrPtr)
    *ptrPtr++ = chrPtr++;

#3


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"

Aditya重新提交了你的答案。我不确定你为什么要分配所有的指针。 “如果我有一系列的char指针”

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.

当然这些指针只有在静态str数组存在之前才有效(通常直到你退出语句块)。非常容易出错,但这正是您的要求。

#4


"If I have an array of char pointers"

“如果我有一系列的char指针”

char** arr;

"and another static array of characters"

“和另一个静态字符数组”

char str[]="Hi there"

char str [] =“你好”

"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.

希望能帮助到你。

#5


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;
}

#1


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

我在这里即兴创作Tomi的答案。其他人对于这样的问题来说太长了。

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.

因为我使用了sizeof,所以你在这里得到第4个指针,它指向字符串的NULL终止字符。

#2


char  chrArr[] = "asd";
char* ptrArr[strlen(chrArr)];

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

while (*chrPtr)
    *ptrPtr++ = chrPtr++;

#3


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"

Aditya重新提交了你的答案。我不确定你为什么要分配所有的指针。 “如果我有一系列的char指针”

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.

当然这些指针只有在静态str数组存在之前才有效(通常直到你退出语句块)。非常容易出错,但这正是您的要求。

#4


"If I have an array of char pointers"

“如果我有一系列的char指针”

char** arr;

"and another static array of characters"

“和另一个静态字符数组”

char str[]="Hi there"

char str [] =“你好”

"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.

希望能帮助到你。

#5


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;
}