SeahawksRdaBest
SeahawksRdaBest

Reputation: 886

sizeof operation in c & strings

I am writing a C program to understand strings and pointers. Everything works except the sizeof operation for a char* [] and char**[].

Here is my code:

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;

}

So how do get the number of elements in char* [] and char** [] ? Also on another note if I for example declare char*[] src2 = {"rabbit","dog","monkey"}; as only char*[] m_src. Then do I have to malloc space for each element I add into this array ? for example

If I had instead done

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

Upvotes: 0

Views: 197

Answers (3)

smRaj
smRaj

Reputation: 1306

How do I get the number of elements in char *[] and char **[] ?

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

Same technique for char **[] too,

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

How do I alloc dynamic Array to point to 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]);

Why the below code doesn't work ?

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]);

You wanted to dynamically allocate space for char *. So, pass malloc() the sizeof *src5 and not the sizeof src5. src5 is the pointer to char *'s , you will use later to access the dynamically allocated space.

src5 = malloc(3 * sizeof *src5); 

*src5 is of type char *.

Upvotes: 2

anomaaly
anomaaly

Reputation: 841

You'll need to divide the size of the array, in bytes, by the size of a single element:

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

Upvotes: 2

Leigh
Leigh

Reputation: 12526

sizeof is only working that way because you're creating a static string, assigning it at creation.

sizeof will show the size of the data type normally (if you use the pointer method), even after you malloc() the memory. Execute this code to demonstrate:

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

For your string lengths, you're probably after strlen(). When you malloc you also need to check whether it returns NULL (if it fails) and also free later. The size is usually a buffer, so to store 50 char in something you're allocating with malloc, you'd use something like this (though the sizeof part isn't needed for char):

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

Upvotes: 2

Related Questions