Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to assign a value to a string array?

Tags:

c

string

for example

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char substr[10][20];

int main() {
    substr[0] = "abc";

    printf("%d", substr[0]);
}

of course above is wrong? how to do it? thanks

like image 587
friends Avatar asked Feb 22 '26 01:02

friends


1 Answers

You can't assign strings like that in C. Instead, use strcpy(substr[0], "abc"). Also, use %s not %d in your printf

like image 107
Gabi Purcaru Avatar answered Feb 23 '26 15:02

Gabi Purcaru



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!