Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

There is a difference between an initialiser and an assignment. They both are introduced by =, but they are syntactically and semantically different.

For an initializer, C can deduce the type of the right side of the = by the left side type. For an assignment, the type must be know with the right side and be compatible with the left side (lvalue) type. Most imporatnt here: the compiler has no type information from the lvalue when evaluating the right side (it does not even started the assignment expression at that moment).

An initialiser must only be used with a definition (you seem to confuse this with declaration - pay heed, they are well-defined terms with different meaningdifferent meaning). SO once you have complete the definition, you cannout use an _initialiser anymore.

Since C99, you can use a compound literal. This is much like a string literal ("Hello world"), but you have to tell the compiler its exact type (the string literal's type is given by the "). Additionally, C does not allow arrays to be simply assigned like scalars or structs. Thus you have to memcpy instead of the assignment:

memcpy(matrix[0], (char [10]){ 1,2,3,4, ... }, 10));

Note the (char ...} part which is the compound literal. This is not a cast, but tells the compiler which type the object in the curly braces has (here: array of 10 chars). A cast OTOH is a prefix expression and changess the type of the following expression. Note that you can use a _string literal here instead, but that does obviously not work for other array element types. Also note that for string literals you cannot define their length other than providing as many characters. So if you forget two characters, you will invoke undefined behaviour for reading outside array boundaries. (Two because of the implicit trailing '\0' character).

For the dimensions, you better use symbolic constants, (i.e. macros in C) like #define ROWS 10 to avoid magic numbers in your code - you have to use these values multiple times.

For an array of pointers, you can use the easier way:

matrix[0] = (char [10]){ 1,2,3,4,...};

Warning: This does not allow to change entries like the first version does, because literals must not be written to!

Extra: This version also works for structs (not just pointers to them), because C does allow assignments for those. To me, this looks like one of the irregularities of C one has to live with.

There is a difference between an initialiser and an assignment. They both are introduced by =, but they are syntactically and semantically different.

For an initializer, C can deduce the type of the right side of the = by the left side type. For an assignment, the type must be know with the right side and be compatible with the left side (lvalue) type. Most imporatnt here: the compiler has no type information from the lvalue when evaluating the right side (it does not even started the assignment expression at that moment).

An initialiser must only be used with a definition (you seem to confuse this with declaration - pay heed, they are well-defined terms with different meaning). SO once you have complete the definition, you cannout use an _initialiser anymore.

Since C99, you can use a compound literal. This is much like a string literal ("Hello world"), but you have to tell the compiler its exact type (the string literal's type is given by the "). Additionally, C does not allow arrays to be simply assigned like scalars or structs. Thus you have to memcpy instead of the assignment:

memcpy(matrix[0], (char [10]){ 1,2,3,4, ... }, 10));

Note the (char ...} part which is the compound literal. This is not a cast, but tells the compiler which type the object in the curly braces has (here: array of 10 chars). A cast OTOH is a prefix expression and changess the type of the following expression. Note that you can use a _string literal here instead, but that does obviously not work for other array element types. Also note that for string literals you cannot define their length other than providing as many characters. So if you forget two characters, you will invoke undefined behaviour for reading outside array boundaries. (Two because of the implicit trailing '\0' character).

For the dimensions, you better use symbolic constants, (i.e. macros in C) like #define ROWS 10 to avoid magic numbers in your code - you have to use these values multiple times.

For an array of pointers, you can use the easier way:

matrix[0] = (char [10]){ 1,2,3,4,...};

Warning: This does not allow to change entries like the first version does, because literals must not be written to!

Extra: This version also works for structs (not just pointers to them), because C does allow assignments for those. To me, this looks like one of the irregularities of C one has to live with.

There is a difference between an initialiser and an assignment. They both are introduced by =, but they are syntactically and semantically different.

For an initializer, C can deduce the type of the right side of the = by the left side type. For an assignment, the type must be know with the right side and be compatible with the left side (lvalue) type. Most imporatnt here: the compiler has no type information from the lvalue when evaluating the right side (it does not even started the assignment expression at that moment).

An initialiser must only be used with a definition (you seem to confuse this with declaration - pay heed, they are well-defined terms with different meaning). SO once you have complete the definition, you cannout use an _initialiser anymore.

Since C99, you can use a compound literal. This is much like a string literal ("Hello world"), but you have to tell the compiler its exact type (the string literal's type is given by the "). Additionally, C does not allow arrays to be simply assigned like scalars or structs. Thus you have to memcpy instead of the assignment:

memcpy(matrix[0], (char [10]){ 1,2,3,4, ... }, 10));

Note the (char ...} part which is the compound literal. This is not a cast, but tells the compiler which type the object in the curly braces has (here: array of 10 chars). A cast OTOH is a prefix expression and changess the type of the following expression. Note that you can use a _string literal here instead, but that does obviously not work for other array element types. Also note that for string literals you cannot define their length other than providing as many characters. So if you forget two characters, you will invoke undefined behaviour for reading outside array boundaries. (Two because of the implicit trailing '\0' character).

For the dimensions, you better use symbolic constants, (i.e. macros in C) like #define ROWS 10 to avoid magic numbers in your code - you have to use these values multiple times.

For an array of pointers, you can use the easier way:

matrix[0] = (char [10]){ 1,2,3,4,...};

Warning: This does not allow to change entries like the first version does, because literals must not be written to!

Extra: This version also works for structs (not just pointers to them), because C does allow assignments for those. To me, this looks like one of the irregularities of C one has to live with.

added 1186 characters in body
Source Link

There is a difference between an initialiser and an assignment. They both are introduced by =, but they are syntactically and semantically different.

For the firstan initializer, C can deduce the type of the right side of the = by the left side type.

  For an _assignmentassignment, the type must be know with the right side and be compatible with the left side (lvalue) type. Most imporatnt here: the compiler has no type information from the lvalue when evaluating the right side (it does not even started the assignment expression at that moment).

An initialiser must only be used with a definition (you seem to confuse this with declaration - pay heed, they are well-defined terms with different meaning). SO once you have complete the definition, you cannout use an _initialiser anymore.

Since C99, you can use a compound literal and. This is much like a string literal - as("Hello world"), but you have to tell the compiler its exact type (the string literal's type is given by the "). Additionally, C does not allow arrays to be simply assigned like scalars or -structs. Thus you have to memcpy instead of the assignment:

memcpy(matrix[0], (char [10]){ 1,2,3,4, ... }, 10));

Note the (char ...} part which is the compound literal. This is not a cast, but tells the compiler which type the object in the curly braces enclose, while ahas (here: array of 10 chars). A cast OTOH is an expression. The version witha string literalsprefix expression also works (just replaceand changess the whole compound literal)type of the following expression. HoweverNote that you can use a _string literal here instead, but that does obviously not work for other array element types. Also note that for thosestring literals you cannot define thetheir length, so other than providing as many characters. So if you forget two characters, you will invoke undefined behaviour because offor reading outside array boundsboundaries. (Two because of the implicit trailing '\0' character).

Note thatFor the dimensions, you better use symbolic constants, resp(i.e. macros in C) like #define ROWS 10 to avoid magic numbers in your code - you have to use these values multiple times.

For an array of pointers, you can use the easier way:

matrix[0] = (char [10]){ 1,2,3,4,...};

Warning: This does not allow to change entries like the first version does, because literals must not be written to!

Extra: This version also works for structs (not just pointers to them), because C does allow assignments for those. To me, this looks like one of the irregularities of C one has to live with.

There is a difference between an initialiser and an assignment. For the first, C can deduce the type of the right side of the = by the left side type.

  For an _assignment, the type must be know with the right side.

Since C99, you can use a compound literal and - as C does not allow arrays to be assigned - memcpy:

memcpy(matrix[0], (char [10]){ 1,2,3,4, ... }, 10));

This is not a cast, but tells the compiler which type the curly braces enclose, while a cast is an expression. The version with string literals also works (just replace the whole compound literal). However, note that for those you cannot define the length, so if you forget two characters, you will invoke undefined behaviour because of reading outside array bounds. (Two because of the implicit trailing '\0' character).

Note that you better use constants, resp. macros like #define ROWS 10 to avoid magic numbers in your code.

For an array of pointers, you can use the easier way:

matrix[0] = (char [10]){ 1,2,3,4,...};

Warning: This does not allow to change entries like the first version does, because literals must not be written to!

Extra: This version also works for structs, because C does allow assignments for those. To me, this looks like one of the irregularities of C one has to live with.

There is a difference between an initialiser and an assignment. They both are introduced by =, but they are syntactically and semantically different.

For an initializer, C can deduce the type of the right side of the = by the left side type. For an assignment, the type must be know with the right side and be compatible with the left side (lvalue) type. Most imporatnt here: the compiler has no type information from the lvalue when evaluating the right side (it does not even started the assignment expression at that moment).

An initialiser must only be used with a definition (you seem to confuse this with declaration - pay heed, they are well-defined terms with different meaning). SO once you have complete the definition, you cannout use an _initialiser anymore.

Since C99, you can use a compound literal. This is much like a string literal ("Hello world"), but you have to tell the compiler its exact type (the string literal's type is given by the "). Additionally, C does not allow arrays to be simply assigned like scalars or structs. Thus you have to memcpy instead of the assignment:

memcpy(matrix[0], (char [10]){ 1,2,3,4, ... }, 10));

Note the (char ...} part which is the compound literal. This is not a cast, but tells the compiler which type the object in the curly braces has (here: array of 10 chars). A cast OTOH is a prefix expression and changess the type of the following expression. Note that you can use a _string literal here instead, but that does obviously not work for other array element types. Also note that for string literals you cannot define their length other than providing as many characters. So if you forget two characters, you will invoke undefined behaviour for reading outside array boundaries. (Two because of the implicit trailing '\0' character).

For the dimensions, you better use symbolic constants, (i.e. macros in C) like #define ROWS 10 to avoid magic numbers in your code - you have to use these values multiple times.

For an array of pointers, you can use the easier way:

matrix[0] = (char [10]){ 1,2,3,4,...};

Warning: This does not allow to change entries like the first version does, because literals must not be written to!

Extra: This version also works for structs (not just pointers to them), because C does allow assignments for those. To me, this looks like one of the irregularities of C one has to live with.

added 443 characters in body
Source Link

There is a difference between an initialiser and an assignment. For the first, C can deduce the type of the right side of the = by the left side type.

For an _assignment, the type must be know with the right side.

Since C99, you can use a compound literal and - as C does not allow arrays to be assigned - memcpy:

memcpy(matrix[0], (char [10]){ 1,2,3,4, ... }, 10));

This is not a cast, but tells the compiler which type the curly braces enclose, while a cast is an expression. The version with string literals also works (just replace the whole compound literal). However, note that for those you cannot define the length, so if you forget two characters, you will invoke undefined behaviour because of reading outside array bounds. (Two because of the implicit trailing '\0' character).

Note that you better use constants, resp. macros like #define ROWS 10 to avoid magic numbers in your code.

For an array of pointers, you can use the easier way:

matrix[0] = (char [10]){ 1,2,3,4,...};

Warning: This does not allow to change entries like the first version does, because literals must not be written to!

Extra: This version also works for structs, because C does allow assignments for those. To me, this looks like one of the irregularities of C one has to live with.

There is a difference between an initialiser and an assignment. For the first, C can deduce the type of the right side of the = by the left side type.

For an _assignment, the type must be know with the right side.

Since C99, you can use a compound literal and - as C does not allow arrays to be assigned - memcpy:

memcpy(matrix[0], (char [10]){ 1,2,3,4, ... }, 10));

This is not a cast, but tells the compiler which type the curly braces enclose, while a cast is an expression.

Note that you better use constants, resp. macros like #define ROWS 10 to avoid magic numbers in your code.

For an array of pointers, you can use the easier way:

matrix[0] = (char [10]){ 1,2,3,4,...};

Extra: This version also works for structs, because C does allow assignments for those. To me, this looks like one of the irregularities of C one has to live with.

There is a difference between an initialiser and an assignment. For the first, C can deduce the type of the right side of the = by the left side type.

For an _assignment, the type must be know with the right side.

Since C99, you can use a compound literal and - as C does not allow arrays to be assigned - memcpy:

memcpy(matrix[0], (char [10]){ 1,2,3,4, ... }, 10));

This is not a cast, but tells the compiler which type the curly braces enclose, while a cast is an expression. The version with string literals also works (just replace the whole compound literal). However, note that for those you cannot define the length, so if you forget two characters, you will invoke undefined behaviour because of reading outside array bounds. (Two because of the implicit trailing '\0' character).

Note that you better use constants, resp. macros like #define ROWS 10 to avoid magic numbers in your code.

For an array of pointers, you can use the easier way:

matrix[0] = (char [10]){ 1,2,3,4,...};

Warning: This does not allow to change entries like the first version does, because literals must not be written to!

Extra: This version also works for structs, because C does allow assignments for those. To me, this looks like one of the irregularities of C one has to live with.

added 315 characters in body
Source Link
Loading
added 315 characters in body
Source Link
Loading
added 1 character in body
Source Link
Loading
Rollback to Revision 1
Source Link
Loading
edited body
Source Link
clearlight
  • 12.7k
  • 11
  • 61
  • 82
Loading
Source Link
Loading