开发者

C Struct Compile Error

开发者 https://www.devze.com 2023-01-29 13:26 出处:网络
Why does the following cod开发者_开发知识库e produce a compile-time error? I cannot seem to see why the types are mismatched.

Why does the following cod开发者_开发知识库e produce a compile-time error? I cannot seem to see why the types are mismatched.

typedef char f_string[MAX_CHARS+1] ;    /* string for each field */

/*
 * A parsed CSV line, with the number of fields and upto MAX_FIELDS themselves.
*/

typedef struct {
    int nfields ;               /* 0 => end of file */
    f_string field[MAX_FIELDS] ;        /* array of strings for fields */
} csv_line;

....

csv_line sut;
sut.field[0] = "Name, "; //Compile-time error.

Error being:

error: incompatible types in assignment


You are trying to assign a const char * to a char[], which is not quite the same thing. This would work if your f_string were defined as

typedef const char * f_string;

What you are looking for here is

strcpy ( sut.field[0], "Name, " );

Or use strncpy so that you can specify the size of the destination buffer ..

strncpy ( sut.field[0], "Name, ", MAX_CHARS )

That will keep you from overrunning your buffer.


You'll need to use something like:

strcpy( sut.field[0],"Name, ");

You can't assign strings like you tried other except as an initializater at declaration time.


the type of sut.field[0] is array-of-char of size MAX_CHARS+1 - you cannot assign a string pointer to an array of characters.

You'd either need to change the type of csv_line::field to a const char*, or just do a string copy of the literal "Name, " to the target array.

Note that both strcpy() and strncpy() alone are unsafe: the first might overflow your buffer, and the second might leave it without a NUL terminator. You must be aware of BOTH of these circumstances even if you "know" that your string in question won't ever overflow.

Use a helper function to do this safely:

char * strncopy(char *dst, const char *src, int dstsize)
{
    strncpy(dst, src, dstsize-1);
    dst[dstsize-1] = '\0';

    return dst;
}

Then:

strncopy(sut.field[0], "Name, ", sizeof sut.field[0]);


sut.field[0] is a char[MAX_CHARS+1]

"Name, " is a const char*

Try this:

strcpy(sut.field[0], "Name, ");


The type of sut.field[0] is indeed char [MAX_CHARS+1]. However, most of the other answers have the type of "Name, " wrong - it is actually of type char [7] (use sizeof "Name, " for an easy demonstration of this).

Nonetheless, you still cannot directly assign a char [7] to a char [MAX_CHARS+1]. You cannot even directly assign a char [7] to another char [7] (initialisation is treated differently from assignment in this way).

The answer is probably just to use a coyping function - for example, if you are certain that MAX_CHARS >= 6, then you can just use strcpy(). If you cannot be sure about the length being correct, then you can use strncat() as as truncating string copy:

sut.field[0][0] = '\0';
strncat(sut.field[0], "Name, ", MAX_CHARS);

(Note that despite the name, strncpy() is not suitable for this, and in fact is very rarely the desired function at all).


It is worth pointing out, however, that you can indirectly assign arrays (of the same type) if they are wrapped up inside a struct. This means that the following will work (if you have a C99 compiler):

typedef struct { char s[MAX_CHARS+1] } f_string;    /* string for each field */

csv_line sut;
sut.field[0] = (f_string){"Name, "};
0

精彩评论

暂无评论...
验证码 换一张
取 消