-2

My code is:

char array[5];
array[0] = 'F';
array[1] = '5';
array[2] = ' ';
array[3] = 'D';
array[4] = '3';

printf("%s", array);

printf("\n");

char aa[2][2];
char aaa[2];

aa[0][0] = array[0];
aa[0][1] = array[1];
aa[1][0] = array[3];
aa[1][1] = array[4];

aaa[0] = array[0];
aaa[1] = array[1];

printf("aa[0] %s\n", aa[0]);
printf("aa[0][0] %c\n", aa[0][0]);
printf("aa[0][1] %c\n", aa[0][1]);

printf("aa[1] %s\n", aa[1]);
printf("aa[1][0] %c\n", aa[1][0]);
printf("aa[1][1] %c\n", aa[1][1]);
printf("aaa %s", aaa);

Output is:

F5 D3▒▒▒▒
aa[0] F5D3F5 D3▒▒▒▒
aa[0][0] F
aa[0][1] 5
aa[1] D3F5 D3▒▒▒▒
aa[1][0] D
aa[1][1] 3
aaa F5F5D3F5 D3▒▒▒▒

Could you, please, explain, what does it do? Why does it do that? How to fix it and why is that fix working and this solution not? (...and what is this "▒" symbol actually?)

Thank you!

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Martin Melichar
  • 1,036
  • 2
  • 10
  • 14

1 Answers1

0

your string isn't terminated by '\0' character, thats why pritnf prints you next random characters

change you code

char array[5];
array[0] = 'F';
array[1] = '5';
array[2] = ' ';
array[3] = 'D';
array[4] = '3';

to

char array[6] = "F5 D3\0";

it should work

colorgreen
  • 265
  • 1
  • 2
  • 12
  • 4
    You don't need the \0 in your code above. It's redundant as C string literals always have a \0 at the end. – john Jul 14 '18 at 14:06
  • It also won't compile because the array dimension is therefore wrong. – Lightness Races in Orbit Jul 14 '18 at 14:08
  • it is compiling – colorgreen Jul 14 '18 at 14:10
  • 1
    "your string isn't terminated by '\0' character" is amiss. "your character array isn't terminated by '\0' character" would be better. By definition a _string_ always has a _null character_, else it is not a string. – chux - Reinstate Monica Jul 14 '18 at 14:10
  • @chux _"By definition a string always has a null character, else it is not a string."_ What about `bstr_t` or `std::string` then? – πάντα ῥεῖ Jul 14 '18 at 14:35
  • @chux That's not true. There are other ways of managing a string than terminating it with `\0`. That just happens to be the method the C standard library chose. – Galik Jul 14 '18 at 14:36
  • @πάνταῥεῖ Title indicates C "How to assign array values to another arrays in C". C++ tag - which I did not notice til now - should be removed. – chux - Reinstate Monica Jul 14 '18 at 14:36
  • @chux Well, `bstr_t` can be interfaced in plain c code. – πάντα ῥεῖ Jul 14 '18 at 14:38
  • 1
    @Galik `bstr_t` is not part of standard C nor the standard library which specifies "A string is a contiguous sequence of characters terminated by and including the first null character." – chux - Reinstate Monica Jul 14 '18 at 14:40
  • @chux I'm just referring to the definition of a *string* which is simply a sequence of characters. Some character encodings could require null values in the middle of a string (for example). Then the C standard library functions would be insufficient. – Galik Jul 14 '18 at 14:42
  • @Galik: The question is about C. Without further information, this implies "string" refers to the C standard specification of a string. Which is a`char []` with NUL-terminator, nothing else. – too honest for this site Jul 14 '18 at 14:46
  • 1
    @Galik I am referring to the definition of a _string_ as C uses it as this is a C question. C does not define _string_ as "simply a sequence of characters" - it is more restrictive. If the question was about general programming, your personal definition may apply yet for C code and posts, using C's definitions makes most sense. – chux - Reinstate Monica Jul 14 '18 at 14:46
  • @chux Well it seems the language of this question changed from `C++` to `C`. :) – Galik Jul 14 '18 at 14:48