Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7039667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:49:34+00:00 2026-05-28T01:49:34+00:00

I want to use an array of string the same as this: char arr[][20]

  • 0

I want to use an array of string the same as this:

char arr[][20] = {"username1", "username2"};

after i have not problem to get values, for example :

printf("%s", arr[0]); // for "username1"

i have problem to insert new string to this array, something like this!? :

arr[2] = "username3"; // or sprintf(arr[2], "%s", "username3");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:49:35+00:00Added an answer on May 28, 2026 at 1:49 am

    The wrong way of doing this would be as follows:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void) 
    {
        char arr[][20] = {"username1", "username2"};
        printf("%s\n", arr[0]);
        printf("%s\n", arr[1]);
        printf("sizeof arr : %d \n", sizeof(arr)/sizeof(*arr));
        strcpy(arr[2],"username3");
        printf("%s\n", arr[2]);
        printf("sizeof arr : %d \n", sizeof(arr)/sizeof(*arr));
        return 0;
    }
    

    because, when you write something like the below

    char arr[][20] = {"username1", "username2"};
    

    The first dimension is automatically calculated based on the given initializer, in this case, it is 2 (i.e. index 0 and 1).

    Later when you try to access (read or write), the third element i.e. index 2, it is a undefined behavior.

    If you want to increase the first dimension of a 2d array, the correct way of doing that would be as follows:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define LEN 20
    
    int main(void) 
    {
        char **arr = NULL;
        int n=2, n1;
        int i;
    
        if ((arr = malloc(sizeof(char *) * n)) == NULL) {
            printf("unable to allocate memory \n");
            return -1;
        }
    
        for (i=0; i<n ; i++) {
            if ((arr[i] = malloc(sizeof(char) * LEN)) == NULL) {
                printf("unable to allocate memory \n");
                return -1;
            }
        }
    
        strcpy(arr[0], "username1");
        strcpy(arr[1], "username2");
    
        printf("%s\n", arr[0]);
        printf("%s\n", arr[1]);
    
        /* add 5 more elements to arr */
        n1 = 5;
    
        if ((arr = realloc(arr, sizeof(char *) * (n + n1))) == NULL) {
            printf("unable to allocate memory \n");
            return -1;
        }
    
        for (i=n; i<n1+n ; i++) {
            if ((arr[i] = malloc(sizeof(char) * LEN)) == NULL) {
                printf("unable to allocate memory \n");
                return -1;
            }
        }
    
        strcpy(arr[2], "username2");
        strcpy(arr[3], "username3");
        strcpy(arr[4], "username4");
        strcpy(arr[5], "username5");
        strcpy(arr[6], "username6");
    
        /* if you uncomment the below, it will lead to undefined behavior */
        //strcpy(arr[7], "username7");
    
        for (i=0; i<n+n1 ; i++)
            printf("%d : %s \n", i, arr[i]);
    
        for (i=0; i<n+n1 ; i++) 
            free(arr[i]);
        free(arr);
    
        return 0;
    }
    
    $ ./a.out 
    username1
    username2
    0 : username1 
    1 : username2 
    2 : username2 
    3 : username3 
    4 : username4 
    5 : username5 
    6 : username6 
    $ 
    

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of strings plus one additional string. I want to use
I use this code to get a String array of headings used in a
I have an array of strings that I want to use for button titles
I have a string array and I need to use the first string in
I have this method which return an array of string : private static string[]
I want to take an array and use that array's values to populate an
Why would someone want to use a linked-list over an array? Coding a linked-list
When I want an array of flags it has typically pained me to use
i want use some data from a website with web service. i have a
I have a transaction log file in CSV format that I want use to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.