I'm trying to assign two strings in a string array but the compiler won't let me do it
typedef char string[20];
string dizionario[2];
dizionario[0] = "ergbeciao";
dizionario[1] = "ciaozio";
I'm trying to assign two strings in a string array but the compiler won't let me do it
typedef char string[20];
string dizionario[2];
dizionario[0] = "ergbeciao";
dizionario[1] = "ciaozio";
Initialize the array variable while declaring. In your program you are using double dimmesion of char array.I hope that u will understand and it will work
#include<stdio.h>
int main()
{
typedef char string[20];
int i;
string dizionario[2]={ "ergbeciao", "ciaozio"};
for (i=0;i<2;i++)
{
printf("\nSting i :%s",i,dizionario[i]);
}
}
Output
String 0 : ergbeciao
String 1 : ciaozio
Your code could work if you changed the definition of string
to something more useful:
typedef const char *string;
int main() {
string dizionario[2];
dizionario[0] = "ergbeciao";
dizionario[1] = "ciaozio";
}
Note that literal strings ("ergbeciao"
) are immutable.
You cannot use the =
operator to copy the contents of one string (or any array type) to another; you must use the strcpy
library function instead:
strcpy(dizionario[0], "ergbeciao");
strcpy(dizionario[1], "ciaozio");
This is a function of how C treats array expressions.
First and most importantly, an array expression may not be the target of an assignment (it is not a modifiable lvalue). dizionario[0]
and dizionario[1]
are array expressions of type char [20]
; by this simple virtue, they may not be the target of the =
operator.
Secondly, unless it is the operand of the sizeof
or unary &
operators, or is a string literal used to initialize a character array in a declaration, an expression of type "N-element array of T
" will be converted ("decay") to an expression of type "pointer to T
", and the value of the expression is the address of the first element of the array. When you write
dizionario[0] = "ergbeciao";
the compiler converts the expression "ergbeciao"
from type "10-element array of char
" to "pointer to char
", and the value of the expression is the address of the first element.
You can use a string literal as an initializer in a declaration:
string dizionario[2] = { "ergbeciao", "ciaozio" };
In this case, the contents of the string literals will be copied to the array elements as you expect. But this only works as part of a declaration.
First of all you can not directly assign value in string by assignment operator you should have to do string operation like this strcpy(dizionario[0] , "ergbeciao");
and strcpy(dizionario[1] , "ciaozio");
otherwise you have to directly assign it while declare that string.
your code should be like this
#include<stdio.h>
#include<string.h>
void main(){
typedef char string[20];
string dizionario[2];
strcpy(dizionario[0] , "ergbeciao");
strcpy(dizionario[1] , "ciaozio");
printf("%s\n %s\n",dizionario[0],dizionario[1]);
}
your output is:
ergbeciao
ciaozio
cheers..........
This depends upon what you want to accomplish.
If you want to declare and allocate two, contiguous strings of 20 characters, fixed width, there are two approaches,
#define STRMAX (20)
typedef char string[STRMAX+1]; //need room for null terminator
string dizionario[2] = { "ergbeciao", "ciaozio" }; //array of char[20+1]
or you can allocate, then assign values using strcpy,
string dizionario[2] = { 0 }; //empty
strcpy( dizionario[0], "ergbeciao" );
strcpy( dizionario[1], "ciaozio" );
However, you may want an array of pointers to strings, so define differently,
#define STRMAX (20)
typedef char string[STRMAX+1]; //need room for null terminator
string* dizionario[2] = { 0 }; //array of null pointers
Then assign the literal strings (pointers) to the pointer array,
dizionario[0] = "ergbeciao";
dizionario[1] = "ciaozio";
Or provide distinct memory using malloc,
dizionario[0] = malloc("ergbeciao");
dizionario[1] = malloc("ciaozio");
Better, provide constructor and destructor for string,
string*
stringNew(char* sp)
{
char* np = malloc(STRMAX+1);
if(!np) return np; //malloc failed
if(sp) strcpy(np,sp); //null source pointer
return np;
}
void
stringDel(string* sp)
{
if(!sp) return; //avoid double free
free(sp);
return(sp);
}
dizionario[0] = stringNew("ergbeciao");
dizionario[1] = stringNew("ciaozio");