skip to Main Content

I am dealing with an issue about multidimensional char array in C. After a time break it is hard for me for remembering the all. Try to figure out what I am doing the wrong. Platform is Ubuntu 21.10, compiler is gcc, the code is below. Any help
I would be much appreciated.

#include <stdio.h>

int main() {
    char *a1 = "hello1";
    char *a2 = "hello2";
    char b[][2] = { a1, a2 };
    printf("%sn", b[1]);
    getchar();
    return (0);
}

5

Answers


  1. Chosen as BEST ANSWER

    It's me again

    char d1[]="hello World";//This syntax is true
    char *d2="Hello World";// This is another appearance of previous line
    char *a1="hello1";
    char *a2="hello2";
    char *c1[] ={a1,a2}; //This line is also working
    char **c2={a1,a2};   /*Right part of the assignment operator is data.
    Can we assing data to c2 like this, I am a little confused. Is that correct*/
    

    Thanks for assist


  2. Because a1 and a2 are pointers, you need an array of pointers to contain them:

    const char *b[] = { a1, a2 };  // char *b[2] = { a1, a2 } would also work
    

    So what you need isn’t really an array of arrays, but just a standard single one-dimensional array.

    Also note that I added the const modifier, to make it an array of pointers to constant characters. That’s because it’s not allowed to modify literal strings, so all pointers to them should be made constant.


    If you really want an array of arrays, for example so you would be able modify the strings, you first of all need to make sure that the dimensions are correct. You have two strings, so you need an array of two elements. Then you need the second "dimension" to be large enough to hold both the strings and all possible additions you might need to do.

    For example:

    char b[2][32];  // Two arrays of 32 characters each
    

    But note that you can’t initialize these arrays using the pointers you have, or even using other arrays. Instead you must explicitly copy the strings into the arrays:

    strcpy(b[0], a1);
    strcpy(b[1], a2);
    
    Login or Signup to reply.
  3. Correction is

    char *b[] ={a1,a2};
    

    Corrected code

    #include<stdio.h>
    int main()
    {
        char *a1="hello1";
        char *a2="hello2";
        char *b[] ={a1,a2};
        printf("%sn",b[0]);
        getchar();
        return(0);
    }
    
    Login or Signup to reply.
  4. Array of pointer is not a 2D array.

    This defines and initializes 2D char array:

    char b[][7] = {"Hello1", "Hello2"};
    
    Login or Signup to reply.
  5. also a way:

    #include <stdio.h>
    #include <string.h>
        
    int main() 
    {
        char **matrix;
        int i = 0;
    
        matrix = malloc(sizeof(char *) * 3);
        matrix[0] = strdup("hello");
        matrix[1] = strdup("world");
        matrix[2] = NULL;
    
        while(matrix[i] != NULL)
        {
            printf("%sn", matrix[i]);
            free(matrix[i]);
            i++;
        }
        free(matrix);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search