Array of Strings in C programming

Array of Strings in C programming

Size of an array:

The size of the ss array is 32 because it's an array of pointers to characters (i.e., strings). In C, the size of a pointer is typically 4 or 8 bytes, depending on the architecture (32-bit or 64-bit).
In your case, it seems like you're running this code on a 64-bit system, where the size of a pointer is 8 bytes. The ss array has 3 elements, each of which is a pointer to a string. So, the total size of the ss array is 4 * 8 = 32 bytes. This is why sizeof(ss) is32. The sizeof operator in C returns   the size of the object or type in bytes.

Code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
   
    char s[MAX_NAME] = "Ashiq";
    char* ss[] = {"ash", "che", "ashiq", "chester"};

    int length = sizeof(ss)/sizeof(ss[0]);
    int sizeofarray = sizeof(ss);

    printf("length: %d\n", length);
    printf("Size of array: %d\n", sizeofarray);
   
    // The size of the [ss] array is 32 
   
    for(int i=0; i<length; i++){
        printf("%s\n", ss[i]);
    }

    return 0;
}


Output:

length: 4 Size of array: 32 ash che ashiq chester




Comments

Popular posts from this blog

12 Best Websites to Practice Coding for Beginners

Using Generic in TypeScript about types in the Code

Usage of Common “Header files” in C programming