简体   繁体   中英

Printing char arrays from a list

It's been awhile since I had to deal with C pointers, and I'm having trouble with a project. Here's some example code I made to highlight my problem. I Just need to be able to add strings to the "wordList" and be able to retrieve them later.

// This typedef is in the header file
// typedef char Word[ WORD_LEN + 1 ];

Word wordList[ MAX_WORDS ];

int main () {
  Word message = "HEllO";
  Word message2 = "OHHO";

  *wordList[ 0 ] = malloc ( sizeof( char ) * ( strlen( message ) + 1 ) );
  *wordList[ 0 ] = *message;

  *wordList[ 1 ] = malloc ( sizeof( char ) * ( strlen( message2 ) + 1 ) );
  *wordList[ 1 ] = *message2;

  printf( "%s\n", &wordList[0]);
  printf( "%s\n", &wordList[1]);
}

Currently the words are not printing, it will only print the first letter. Any tips on where I might be messing up would be incredible. Thanks!

Use good ol' strcpy , no need for playing around with pointers.

Word message = "HEllO";
strcpy(wordList[0], message);

printf("%s\n", wordList[0]);

Or even strncpy as @alk pointed out

strncpy(wordList[0], message, sizeof(wordList[0]) - 1);
wordList[0][WORD_LEN] = '\0'; 

wordList is an array of C-"string"s. So use them like this:

Word message = "HEllO";

size_t size_max = sizeof wordList[0] - 1;

strncpy(wordList[0], message, size_max);
wordList[0][size_max] = '\0';

Since a Word is already typedef'd as a char array (C string), there's no need to use malloc to allocate memory in your array. Essentially, wordList is an array of arrays and you can just copy the contents of your strings (Words) into it at the desired indices. See the below code for a working implementation.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// some magic numbers
#define WORD_LEN 50
#define MAX_WORDS 20

typedef char Word[ WORD_LEN + 1 ];
Word wordList[ MAX_WORDS ];

int main () {
    Word message = "HEllO";
    Word message2 = "OHHO";

    //wordList[ 0 ] = malloc ( sizeof( char ) * ( strlen( message ) + 1 ) );
    //*wordList[ 0 ] = *message;
    strncpy(wordList[0], message, sizeof(Word));

    //wordList[ 1 ] = malloc ( sizeof( char ) * ( strlen( message2 ) + 1 ) );
    //*wordList[ 1 ] = *message2;
    strncpy(wordList[1], message2, sizeof(Word));

    printf( "%s\n", wordList[0]);
    printf( "%s\n", wordList[1]);
}

Right now wordList is an array of Word, and each word is an array of chars. When you defined Word wordList[ MAX_WORDS ] the space for MAX_WORDS number of Words of size WORD_LEN+1 has already been allocated in the global section, so you don't need to malloc more space for this.

To insert the message into the word you would use strcpy ( http://www.cplusplus.com/reference/cstring/strcpy/ ) rather than mallocing space and assigning the message. So to insert message into wordList[0] would look like strcpy( wordList[0], message) . Strcpy takes in two char * with the first being the destination of the copy and and second being the source.

Here is some example code of what you should try to do:

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

typedef char Word[ 10 ];
Word wordList[ 10 ];
int main(){
    Word message = "Hello";
    strcpy( wordList[0], message);
    printf("%s", wordList[0]);
    return 0;
}

** To be safer you can use strncpy ( http://www.cplusplus.com/reference/cstring/strncpy/ ) instead of strcpy. Here you can specify the number of characters you want to copy as a parameter. All the code would look the same except you would change strcpy to strncpy and add in a third parameter of how many character to copy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM