Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - Setting a static char array with a "string"

a very simple question I am afraid but I have been stuck for days with this, Google gives me nothing, I even tried bing... ;o)

I am working in pure C under windows in VS2010.

I have a static char array as such...

static char word[5];

I can set each array position just fine i.e...

word[0] = 'f'; word[1] = 'o'; word[2] = 'o';

But what I cannot seem to do (at any point after declaration) is...

word = "foo";

Any help or pointers as to where I am going wrong would be very much appreciated.

Thanks all in advance.

like image 540
Nick Avatar asked Apr 07 '26 06:04

Nick


1 Answers

strncpy(word, "foo", _countof(foo));

If _countof is not defined, use sizeof(foo) / sizeof(*foo) instead.

like image 92
user541686 Avatar answered Apr 09 '26 20:04

user541686



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!