![Quote](https://cboard.cprogramming.com/images/misc/quote_icon.png)
Originally Posted by
Pingu
Can we do this :
Code:
char strings[][100]={"ABC","EFG","IJK","LKM"};
char temp[100];
temp=strings[1];
Please help!
You need to study the C Standard Library that is provided with all C compilers, One of the possible resources is:
The C Library Reference Guide (Section 2.4, "String.h")
Also, if programming on a Linux system, the "man" command is your best friend!
"man 3 strcpy" (Section 3 is for programming.)
You should also be aware that the C String Library is also one of the weakest parts of the programming in C. Please be aware that strcpy(), strcat(), gets(), and others can write beyond the end of the buffer. For example:
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char buff[5];
strcpy(buff, "This is a test of a string larger than the buffer.");
printf("%s\n", buff); /* Segmentation fault! (On my Linux system) */
return 1; /* This program does NOT work correctly! */
}
You solution, with additional error checking could be:
Code:
char strings[][100]={"ABC","EFG","IJK","LKM"};
char temp[100];
/* error check for possible overflow before copying! */
strcpy(temp, strings[1]);