7

Consider the following code.

char message[]="foo";

void main(void){
    message[] = "bar";
}

Why is there a syntax error in MPLAB IDE v8.63? I am just trying to change the value of character array.

Ayush joshi
  • 305
  • 1
  • 5
  • 14
newbie
  • 93
  • 1
  • 1
  • 7
  • 1
    A syntax error is a compilation error, not a runtime error, so this has nothing to do with modifying your char array. You just can't write `message[] = "bar";` with nothing between the brackets. It works with `char message[] = "foo";` because you declare `char message[]` then it assigns as `message = "foo";` – Eregrith Jan 18 '13 at 09:49
  • Thank you for your assistance. I would like to know the proper way of assigning new value to character array. Thanks. – newbie Jan 18 '13 at 09:51
  • As i put it `message = "foo";` is correct – Eregrith Jan 18 '13 at 09:52
  • 3
    It is very important than you learn how arrays and pointers work, before you move on to string handling. – Lundin Jan 18 '13 at 10:14
  • 1
    And memory management in general – Eregrith Jan 18 '13 at 10:22
  • You'll gonna like char* ... – Shark Jan 18 '13 at 10:22
  • 1
    'As i put it message = "foo"; is correct ' -- No, that isn't correct, it's the blind leading the blind. – Jim Balter Jan 18 '13 at 10:26

5 Answers5

16

You cannot use character array like that after declaration. If you want to assign new value to your character array, you can do it like this: -

strcpy(message, "bar");
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Previously, the "foo" shows up in my microcontroller LCD display(I have a LCD display will keep displaying the value of "char message[]"). I did include string.h. However, when I use the method above, nothing shows up. – newbie Jan 18 '13 at 09:59
  • See my comment above. Your µC is very likely a Harvard architecture. Here a list (non exhaustive) of µC using that paradigm: AVR, PIC, 8051, Cortex-M3 etc. – Patrick Schlüter Jan 18 '13 at 10:53
  • I checked, MPLAB is for Microchip, therefore PIC controller which tells it is a Harvard architecture with separate Instruction and data memories. You can not use `strcpy` for litterals. – Patrick Schlüter Jan 18 '13 at 10:56
  • I guess this explains it all. Thanks! – newbie Jan 18 '13 at 11:04
11

Assignments like

message[] = "bar";

or

message = "bar";

are not supported by C.

The reason the initial assignment works is that it's actually array initialization masquerading as assignment. The compiler interprets

char message[]="foo";

as

char message[4] = {'f', 'o', 'o', '\0'};

There is actually no string literal "foo" involved here.

But when you try to

message = "bar";

The "bar" is interpreted as an actual string literal, and not only that, but message is not a modifiable lvalue, ie. you can't assign stuff to it. If you want to modify your array you must do it character by character:

message[0] = 'b';
message[1] = 'a';

etc, or (better) use a library function that does it for you, like strcpy().

Yakov Shklarov
  • 267
  • 2
  • 7
  • one thing weird about microcontroller IDE is when I try strcpy(message, newmessage) - newmessage contains "bar". It works fine. However, if I consider strcpy(message, "bar"), it fails badly. – newbie Jan 18 '13 at 10:31
  • 1
    If your microcontroller has a Harvard architecture then you will need to call specific functions (or even use assembly). On Harvard architecture there are 2 separate address spaces (RAM and ROM). A litteral is generally compiled/linked/mapped to a ROM address. Variables are in the RAM area. Copying data from the ROM to the RAM needs special instructions that the normal `strcpy` does not use. – Patrick Schlüter Jan 18 '13 at 10:45
  • The char assignement method works for Harvard architecture because the chars ('b', 'a') are compiled directly as immediate values in the instructions. – Patrick Schlüter Jan 18 '13 at 10:47
  • I checked, MPLAB is for Microchip, therefore PIC controller which tells it is a Harvard architecture with separate Instruction and data memories. You can not use `strcpy` for litterals. – Patrick Schlüter Jan 18 '13 at 10:56
  • `"foo"` is actually a string literal here. One of the initialization rules of the langauge is that a char array can be initialized from a string literal. – M.M Jul 16 '17 at 03:06
2

you can do that only in the initialisation when you declare the char array

message[] = "bar";

You can not do it in your code

To modify it you can use strcpy from <string.h>

strcpy(message, "bar");
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
0

You cant change the character array like this . If you want to change the value of character array then you have to change it by modifying single character or you can use

strcpy(message,"bar");
Tushar Mishra
  • 177
  • 1
  • 7
  • I tried using this method but nothing shows up for the value of message in microcontroller LCD display. It works fine for "foo" previously. – newbie Jan 18 '13 at 10:10
  • @newbie You're not showing us all your code, which is why you're not getting helped. – Jim Balter Jan 18 '13 at 10:29
0
char message[]="foo";

This statement cause compiler to create memory space of 4 char variable.Starting address of this memory cluster is pointer value of message. address of message is unchangeable, you cannot change the address where it points . In this case, your only chance is changing the data pointed by message.

char* message="foo"

In this time, memory is created to store the address of pointer, so the address where message point can change during execution. Then you can safely do message="bar"

woryzower
  • 956
  • 3
  • 15
  • 22