Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>Hope it helps</p> <p>edit: I kind of hate magic numbers so I almost totally removed them<br> edit: I've added alternatives Tommi Kyntola and I were talking about in the comments</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; // 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) &gt;= 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 &lt; 5; i++) // 5 because that's how many I've populated printf("%s : %s\n", messages[i][MSGNAME], messages[i][MSGTEXT]); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload