0

我正在编写一个 C 程序来理解字符串和指针。除了 char* [] 和 char**[] 的 sizeof 操作之外,一切都有效。

这是我的代码:

int main(){
  puts("");
  char src[]= "rabbit";
  char* src2[] = {"rabbit","dog","monkey"};
  char* src3[] = {"fish","horse","dolphin"};
  char** src4[] = {src2,src3};
  int i,j;

  printf("Size of the char array only is %d\n",sizeof(src));
  printf("Size of the array of string pointers only is %d\n", sizeof(&src2));
  printf("Size of the array of pointers to string pointers only %d\n\n", sizeof(src4));

   puts("Single char array is:");
   for(i = 0; i<sizeof(src)-1; i++){
     printf("%c,",src[i]);
   }

  puts ("\n");

  puts("Array of strings:");
  puts(src2[0]);
  puts(src2[1]);
  puts(src2[2]);
  puts("");

  puts("Printing the char** [] contents ");

  for(i=0; i<2; i++){
    for(j=0; j < 3;j++){
      puts(src4[i][j]);
     }
  }

  puts("");

  return 0;

}

那么如何获取 char* [] 和 char** [] 中的元素数量呢?另外,如果我声明 char*[] src2 = {"rabbit","dog","monkey"}; 仅作为 char*[] m_src。那么我是否必须为添加到此数组中的每个元素分配空间?例如

如果我做了

    // Code segment changed for char*[]
    char* m_src[];
    // could I do this
    m_src = malloc(3 * sizeof(char*));
    m_src[0] = "rabbit";
    m_src[1] = "dog";
    m_src[2] = "monkey";

    /* Even more is there a way to dynamically add elements to the
    array like mallocing space for a single element at a time and tacking it onto the
    array char* m_src? */  
4

3 回答 3

2

如何获得char *[]and中的元素数量char **[]

printf("Number of elements in char* src2[]: %d\n", (sizeof src2)/(sizeof src2[0]) );

同样的技术char **[]

printf("Number of elements in char** src4[]: %d\n", (sizeof src4)/(sizeof src4[0]) );

如何分配动态数组以指向 C-style-strings ?

char **d_alloc;
d_alloc = malloc(3 * sizeof *d_alloc);
if(d_alloc == NULL)
{
    printf("malloc failed !");
    return -1; 
}
d_alloc[0] = "Created";
d_alloc[1] = "Dynamic";
d_alloc[2] = "Arrays";
printf("%s %s %s\n",d_alloc[0], d_alloc[1], d_alloc[2]);

为什么下面的代码不起作用?

char** src5;
src5 = malloc(3 * sizeof(src5)); 
puts("Dynamically allotted char*"); 
src5[0] = "lion"; 
src5[1] = "shark"; 
printf("%s %s\n",src5[0],src[1]);

您想为char *. 所以,通过malloc()sizeof *src5不是sizeof src5. src5是指向char *'s 的指针,稍后您将使用它来访问动态分配的空间。

src5 = malloc(3 * sizeof *src5); 

*src5是类型char *

于 2013-11-03T04:55:24.213 回答
2

sizeof之所以这样工作,是因为您正在创建一个静态字符串,并在创建时分配它。

sizeofmalloc()即使在内存之后,也会正常显示数据类型的大小(如果使用指针方法) 。执行这段代码来演示:

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

int main ()
{
        char *string;
        char *dynamic;
        dynamic = malloc(10 * sizeof(*dynamic));
        if (!dynamic)
            /* Error, malloc() failed. */
            return 1;

        /* Copy something into the string. */
        strcpy(dynamic, "testing");

        printf("sizeof char pointer:     %d\n", sizeof(string));
        printf("sizeof char:             %d\n", sizeof(*string));
        printf("dynamic:                 %s\n", dynamic);
        printf("sizeof dynamic pointer:  %d\n", sizeof(dynamic));
        printf("sizeof dynamic:          %d\n", sizeof(*dynamic));

        free(dynamic);
        return 0;
}

对于您的字符串长度,您可能在strlen(). 当您malloc还需要检查它是否返回NULL(如果失败)以及free稍后。大小通常是一个缓冲区,因此要将 50 个字符存储在您正在分配的内容中malloc,您将使用类似这样的内容(尽管 sizeof 部分不需要char):

char *str;
str = malloc(50 * sizeof(*str));
于 2013-11-03T04:56:26.953 回答
2

您需要将数组的大小(以字节为单位)除以单个元素的大小:

int size = sizeof(src3) / sizeof(*src3);
int size = sizeof(src4) / sizeof(*src4);
于 2013-11-03T04:56:31.150 回答