1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define STRING_LENGTH 20
#define MAX 30

int read_string(char string[], int n);

int main(){
    int i = 0;
    char *name_list[MAX];
    char word[STRING_LENGTH + 1];

    for (;; i++){
        printf("Enter a word.\n");
        read_string(word, STRING_LENGTH);
        if (word[i] == '\0')
            break;
        name_list[i] = malloc(sizeof(char) * 20);
        strcat(name_list[i], word);

    }


}

int read_string(char string[], int n){
    int ch, i = 0;

    while ((ch = getchar()) != '\n')

    if (i < n)
        string[i++] = ch;
    string[i] = '\0';

    return i;
} 

The point of this program is to read in words and place them into an array of pointers for sorting. this is what i have so far, my debugger is saying that the use of strcat is unsafe but I do not know why. It says to use strcat_s but that crashes my program. Any help on how to get this working?

10
  • sorry i forgot to add that if the user enters nothing after the prompt the program ends. Commented Jul 7, 2015 at 20:39
  • i fixed that but the debugger still says that the use of strcat, strcpy, and strcat_s, strcpy_s is unsafe Commented Jul 7, 2015 at 20:42
  • Also, the for loop as it is now is infinite. You should change it to for(i=0; i<MAX; i++) Commented Jul 7, 2015 at 20:43
  • once the user presses enter without entering a word, the loop breaks. Commented Jul 7, 2015 at 20:50
  • @moffeltje This is C; you wish you would get an IndexOutOfBounds error, instead you get far worse things. Commented Jul 7, 2015 at 21:23

2 Answers 2

1

Ok, I tested your code and I came to the following final code that is working for me and does not give me a warning when compiled with -Wall.

Since you are using strcat instead of strcpy, the string stored in words gets added to the data in the array name_list. But because you didn't put all values in that array to 0, it could happen some garbage data is stored in name_list[i] and the words string gets concatenated after that garbage data.

Therefore I used calloc so all values in the memory you allocate are zero. Another way is to just keep malloc but then change strcat() in strcpy().

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define STRING_LENGTH 20
#define MAX 30

int read_string(char string[], int n);

int main(){
    int i;
    char *name_list[MAX];
    char word[STRING_LENGTH + 1];

    for (i = 0; i < MAX; i++){
        printf("\nEnter a word.\n");
        read_string(word, STRING_LENGTH);
        printf("\nword%d=%s", i, word);
        if (strcmp(word, "") == 0)
            break;
        name_list[i] = calloc(STRING_LENGTH + 1, 1);
        strcat(name_list[i], word);
        printf("\nname_list[%d] = %s", i, name_list[i]);

    }
    return 0;

}

int read_string(char string[], int n){
    int ch, i = 0;

    while ((ch = getchar()) != '\n')

    if (i < n)
        string[i++] = ch;
    string[i] = '\0';

    return i;
} 
Sign up to request clarification or add additional context in comments.

4 Comments

thankyou so much,although i still get the warning message, the program doesnt crash
when i print the name list array though get a bunch of odd charaters and not the words
Other possible issues aside, it seems like the calloc() call should be calloc(STRING_LENGTH + 1, 1). To avoid off-by-one problems like this, I suggest perhaps using strdup() instead of an allocate/copy set of operations on strings.
actually the code is fine but i am actually required to use malloc not calloc
1

Use the memcpy() function :

void *memcpy(void *str1, const void *str2, size_t n)

or the strcpy() function :

char *strcpy(char *dest, const char *src)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.