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 8411009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:19:46+00:00 2026-06-10T00:19:46+00:00

Say I have an array char messages[10][2][50]; What is the correct syntax for strcpy,

  • 0

Say I have an array

char messages[10][2][50];

What is the correct syntax for strcpy, in order to get the data into one of the strings (inner most char array of size 50) and then the corresponding convention to supply it to printf via %s?

For that matter, am I declaring the array subscripts in the correct order? It is intended to be 10 lots of, pairs (of 2) strings. Each string being 50 chars wide.

01{{50 chars},{50 chars}}
02{{50 chars},{50 chars}}
...
09{{50 chars},{50 chars}}
10{{50 chars},{50 chars}}

Various internet sources seem to conflict on which subscript to omit and, whatever I try seems to produce unintended results.

e.g. Could you fill in the blanks to the following

strcpy(message???, "Message 1 Part 1");
strcpy(message???, "m1 p2");
strcpy(message???, "m2 p1");
strcpy(message???, "m2 p2");
strcpy(message???, "m3 p1");
strcpy(message???, "m3 p1");
//So on...

int i;
for(i=0;i<10;i++)
    printf("%s, %s\n", message???, message???);

Such that the array has a structure of and holds:

01{{"Message 1 Part 1\0"},{"m1 p2\0"}}
02{{"m2 p1\0"},{"m2 p2\0"}}
01{{"m3 p1\0"},{"m3 p2\0"}}
//So on...

And outputs as such

Message 1 part 1, m2 p2

m2, p2

m3, p3

and so on

  • 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-06-10T00:19:48+00:00Added an answer on June 10, 2026 at 12:19 am

    I just wrote a quick program to show the things you’ve asked about… loading them up at declaration, strncpy into one of them, and then printing them out.

    Hope it helps

    edit: I kind of hate magic numbers so I almost totally removed them
    edit: I’ve added alternatives Tommi Kyntola and I were talking about in the comments

    #include <stdio.h>
    #include <string.h>
    
    // safe string copy macro, terminates string at end if necessary
    // note: could probably just set the last char to \0 in all cases
    // safely if intending to just cut off the end of the string like this
    
    #define sstrcpy(buf, src, size) strncpy(buf, src, size); if(strlen(src) >= size) buf[size-1] = '\0';
    
    #define MSGLIMIT 10
    #define MSGLENGTH 30
    #define MSGFIELDS 2
    #define MSGNAME 0
    #define MSGTEXT 1
    
    
    int main(void) {
        char messages[MSGLIMIT][MSGFIELDS][MSGLENGTH] = { {"bla", "raa"},
                                                          {"foo", "bar"}
                                                        };
        int i;
    
        char *name1 = "name16789012345678901234567890";
        char *text1 = "text16789012345678901234567890";
    
        char *name2 = "name26789012345678901234567890";
        char *text2 = "text26789012345678901234567890";
    
        char *name3 = "name36789012345678901234567890";
        char *text3 = "text36789012345678901234567890";
    
    
        // doesn't set last char to \0 because str overruns buffer
        // undocumented result of running this, but likely to just get the name2 string
        // as that'll be the very next thing in memory on most systems
    
        strncpy(messages[2][MSGNAME], name1, MSGLENGTH); // 2 because it's the next empty one
        strncpy(messages[2][MSGTEXT], text1, MSGLENGTH);
    
        // alternative suggested by Tommi Kyntola
        // printf family are more complicated and so cost more cpu time than strncpy
        // but it's quick and easy anywhere you have string.h and fine most of the time
    
        snprintf(messages[3][MSGNAME], MSGLENGTH, "%s", name2);
        snprintf(messages[3][MSGTEXT], MSGLENGTH, "%s", text2);
    
        // uses the define macro at the top of the page to set the last char to \0 if
        // otherwise not set by strncpy, adds a little weight but still the better option
        // if performance of this section of code is important
    
        sstrcpy(messages[4][MSGNAME], name3, MSGLENGTH);
        sstrcpy(messages[4][MSGTEXT], text3, MSGLENGTH);
    
    
        for(i = 0; i < 5; i++) // 5 because that's how many I've populated
                printf("%s : %s\n", messages[i][MSGNAME], messages[i][MSGTEXT]);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an array of table DDL queries and I want to get
Say you have an array, data, of unknown length. Is there a shorter method
Say I have an array: $before = array(1,2,3,3,4,4,4,5) How would I remove just one
Let's say I have an array : char[] chars={'X','X','m','a','t','t','X','X'}; I want to create String
Lets say i have the following cstring char array[1000]; How i can convert it
say I have the following code: char[5][5] array; for(int i =0; i < 5;
I have a file that I read into a char array. The char array
I have a char array filled with some characters. Let's say I have HelloWorld
Suppose i have array of characters. say char x[100] Now, i take input from
Say I have declared a 2d array like char* array[30][30]; and what I am

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.