c&字符串

时间:2013-11-03 04:25:45

标签: c arrays string pointers

我正在编写一个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。那么我是否需要为我添加到此数组中的每个元素使用malloc空间?例如

如果我改为做了

    // 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? */  

3 个答案:

答案 0 :(得分:2)

如何获取char *[]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样式字符串?

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 src5src5是指向char *的指针,稍后您将使用它来访问动态分配的空间。

src5 = malloc(3 * sizeof *src5); 

*src5的类型为char *

答案 1 :(得分:2)

sizeof只是这样工作,因为你正在创建一个静态字符串,在创建时分配它。

sizeof将正常显示数据类型的大小(如果使用指针方法),即使在malloc()内存之后也是如此。执行此代码以演示:

#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。大小通常是一个缓冲区,所以要在你用malloc分配的东西中存储50个字符,你可以使用这样的东西(尽管char不需要sizeof部分): / p>

char *str;
str = malloc(50 * sizeof(*str));

答案 2 :(得分:2)

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

int size = sizeof(src3) / sizeof(*src3);
int size = sizeof(src4) / sizeof(*src4);