I am hoping somebody could please clarify what I am doing wrong. I am trying to replicate the strcpy function in C.
The exercise requires us to create two loop through a src string and replace the content at each corresponding index at the destination string. My issue is when I create the test function in int main() I initialise a character array and assign it some content. It compiles fine however I get a norminette error:
// Method 1
char str1[5] = "abcde";// Error: DECL_ASSIGN_LINE Declaration and assignation on a single line
char str2[5] = "fghij"; //Error: DECL_ASSIGN_LINE Declaration and assignation on a single line
If I initialise and assign like bellow, Norminette is ok but I get a compilation error:
// Method 2
char str1[5] = "abcde";
char str2[5] = "fghij";
str1[] = "abcde"; // error: expected expression before ‘]’ token ... (with an arrow pointing to ] bracket)
str2[] = "fghij"; // error: expected expression before ‘]’ token ... (with an arrow pointing to ] bracket)
// Method 3
char str1[] = {'a', 'b', 'c', 'd', 'e','\0'}; // Error: DECL_ASSIGN_LINE Declaration and assignation on a single line
char str2[] = {'f', 'g', 'h', 'i', 'j', '\0'};//Error: DECL_ASSIGN_LINE Declaration and assignation on a single line
I have also tried various methods including str[5] = "abcde" after declaration with no success. My question is how can I declare these character arrays to satisfy both the norminette and compiler?
Also is my understand that in C, a character array and a string are interchangeable concepts? Thank you
CodePudding user response:
In method 2:
str1[] = "abcde"; // error:
is an assignment, so it's invalid. The []
syntax can only be used in a definition. More on this below.
Method 3 is fine. Whoever is flagging this is wrong.
In method 1 [and other places]:
char str1[5] = "abcde";
is wrong because it should be:
char str1[6] = "abcde";
to account for the EOS (0x00) string terminator at the end.
An alternate would be:
char str1[] = "abcde";
And, you did this [more or less] in method 3:
char str1[] = {'a', 'b', 'c', 'd', 'e', '\0'};
AFAICT, this was flagged by the external tool (not the compiler?). It is a perfectly valid alternative.
CodePudding user response:
Also is my understand that in C, a character array and a string are interchangeable concepts?
No. In C, a string is a sequence of character values including a 0-valued terminator. The string "abcde"
would be represented by the sequence {'a', 'b', 'c', 'd', 'e', 0}
. That terminator is how the various string handling routines like strlen
and strcpy
know where the end of the string is.
Strings (including string literals like "abcde"
) are stored in arrays of character type, but not every array of character type stores a string - it could be a character sequence with no 0-valued terminator, or it could be a character sequence including multiple 0-valued bytes.
In order to store a string of N characters, the array has to be at least N 1 elements wide:
char str1[6] = "abcde"; // 5 printing characters plus 0 terminator
char str1[6] = "fghij";
You can declare the array without an explicit size, and the size (including the 1 for the terminator) will be determined from the size of the initializer:
char str1[] = "abcde"; // will allocate 6 elements for str1
char str2[] = "fghij";
I've found the documentation for Norminette and ... ugh.
I get that your school wants everyone to follow a common coding standard; that makes it easier to analyze and grade everyone's code. But some of its rules are just plain weird and non-idiomatic and, ironically, encourage bad style. If I'm interpreting it correctly, it wants you to write your initializers as
char str1[]
= "abcde";
or something equally bizarre. Nobody does that.
One way to get around the problem is to not initialize the array in the declaration, but assign it separately using strcpy
:
char str1[6]; // size is required since we don't have an initializer
char str2[6];
strcpy( str1, "abcde" );
strcpy( str2, "fghij" );
You cannot use =
to assign whole arrays outside of a declaration (initialization is not the same thing as assignment). IOW, you can't write something like
str1 = "abcde";
as a statement.
You either have to use a library function like strcpy
or strncpy
(for strings) or memcpy
(for things that aren't strings), or you have to assign each element individually:
str1[0] = 'a';
str1[1] = 'b';
str1[2] = 'c';
...