-2

I have a pointer to a character array foo

char *foo = "abbdefg";

and a regular character array bar.

char bar[10];

bar holds an array of characters that are unique in bar. For example I want bar[0] to equal "a", bar[1] = "b", bar[2] = "d", etc. However When I set bar[0] to foo[0], bar[0] = "abbdefg" and bar[1] = "bbdefg" and so forth.

I know how to print what I want with printf

printf("%c", foo[0]);

give a and

printf("%c", foo[1]);

gives b, but I don't know how to do the equivalent for just setting bar[0] to "a" instead of "abbdefg."

Any advice would really help, thanks.

alex Brock
  • 23
  • 6
  • 1
    https://stackoverflow.com/questions/10186765/what-is-the-difference-between-char-array-vs-char-pointer-in-c – alpharoz Dec 04 '18 at 21:25
  • I apologize I meant to make foo a pointer but made a type, I've edited it and it is now correct. I looked at the other thread but It doesn't help my problem. – alex Brock Dec 04 '18 at 21:30
  • 1
    Post a [mcve] that demonstrates the problem. – klutt Dec 04 '18 at 21:32
  • Did you write a string terminator `'\0'` in `bar` after the character you copied? – Weather Vane Dec 04 '18 at 21:32
  • 2
    Do you know the difference between "a" and 'a' ? – user253751 Dec 04 '18 at 21:40
  • You cannot in any case set `bar[0]` to `"abbdefg."`, except insamuch as a lenient compiler might perform the needed array to pointer to char two-step conversion without the required cast for the back half. And that would almost surely entail substantial information loss. It's unclear what you're actually doing that makes you think you observe different. If you provide a [mcve], however, then we can tell you what you're doing wrong. – John Bollinger Dec 04 '18 at 21:49
  • Print bar the same way: `printf("%c", bar[0])` will print 'a' and so on. – stark Dec 04 '18 at 22:30
  • `foo` is not a pointer to a character array. It's a pointer to a character. (That character happens to be the first element of an array, and we can use pointer arithmetic on `foo` to access the other elements of the array.) A pointer to the array would be declared as `char (*foo)[8]` rather than as `char *foo`. Also, `const char *foo = "abbdefg";` would be safer. – Keith Thompson Dec 04 '18 at 22:35

1 Answers1

1

You're confusing characters and strings, I think.

bar holds an array of characters that are unique in bar. For example I want bar[0] to equal "a", bar[1] = "b", bar[2] = "d", etc. However When I set bar[0] to foo[0], bar[0] = "abbdefg" and bar[1] = "bbdefg" and so forth.

I know how to print what I want with printf

bar[0] cannot equal "a" because bar[0] is a character and "a" is a string (in this case, an array of two characters). bar[0] can equal 'a' which is just the letter a. and bar[1] can equal 'b' and bar[2] can equal 'd' and so on.

When you set bar[0] to foo[0], using bar[0] = foo[0]; for example, bar[0] now holds 'a'. bar[1] still holds whatever it held before.

bar[0] certainly can't hold "abbdefg" because it only fits one character. Most likely, you are accidentally printing out all of the characters in bar, instead of just bar[0], by using %s instead of %c.

Community
  • 1
  • 1
user253751
  • 57,427
  • 7
  • 48
  • 90