0

This is outside the main:

char message_ecran[NUMBER_OF_STRINGS][STRING_LENGTH+1];

And this is my function

 int main(void)
    {
        Init(); 

        int i;
        char texte7[] = "io";

        for (i=0;i<=NUMBER_OF_STRINGS;i++)
            {
            message_ecran[i] = texte7;
            }
    }

I would like to have an array of strings message_ecran, but it does'nt work:

incompatible types in assignment
6
  • You cannot do that in C. Use strcpy_s instead of your assignment operator. Commented Mar 11, 2013 at 8:19
  • 1
    @AnishRam: strcpy_s isn't part of the C standard library. Use strncpy instead. Commented Mar 11, 2013 at 8:21
  • @Zeta, Good point. I didn't know that. Come to think of it, wouldn't memset or memcpy be better then? Commented Mar 11, 2013 at 8:25
  • You should change your for-loop's condition of i <= NUMBER_OF_STRINGS into i < NUMBER... Commented Mar 11, 2013 at 8:36
  • @AnishRam: Well, no. memcpy is a general copy algorithm, while strncpy will stop on \0 or if a specified number of characters has been copied (strncpy = str ing (n characters) c o py) Commented Mar 11, 2013 at 9:08

4 Answers 4

1

strcpy() , implemented in your program.

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

#define NUMBER_OF_STRINGS 3
#define STRING_LENGTH 80

char message_ecran[NUMBER_OF_STRINGS][STRING_LENGTH+1];

int main(void)
{
    int i;
    char texte7[] = "io";

    for (i=0;i<=NUMBER_OF_STRINGS;i++)
    { 
        strcpy(message_ecran[i],texte7);
        puts(message_ecran[i]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You have to use strcpy for copying strings, assignment will not work. Replace

message_ecran[i] = texte7;

with

strcpy(message_ecran[i], texte7);

Comments

1

The operation you're doing now is an assignment of the pointer.

You can't simply assign one string to another, you have to strcpy() to really copy data.

strcpy(message_ecran[i], texte7);

Also you have to be sure that there is enough memory allocated in message_ecran[i] for string that you're trying to copy. Otherwise, you will corrupt the data.

EDIT:

You can check, the following:

if(strlen(texte7) < STRING_LENGTH+1)

Or you can simply use the following function:

char * strncpy ( char * destination, const char * source, size_t num );

strncpy(message_ecran[i], texte7, STRING_LENGTH);

1 Comment

How should I make sure the memory allocated is sufficient ?
0

use strcpy(destination,source) and try

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.