-
problem in structs
Code:
#include<stdio.h>
#include<string.h>
struct list
{
char first[20];
char name[20];
char blahblah[40];
};
void get_struct(struct list * x);
main()
{
struct list data[4];
struct list *ptr;
ptr=data;
get_struct(ptr);
}
void get_struct(struct list *x)
{
int i;
for(i=0;i<4;i++)
{
((x+i)->first)="storm";
printf("\nfisrt=%s",(x+i)->first);
printf("\nname=%s",(x+i)->name);
printf("\nlif is =%s",(x+i)->blahblah);
strcat((x+i)->first,(x+i)->name);
printf("\nme =%s",(x+i)->first);
}
printf("Good Night");
}
compiler give me this in line
Code:
((x+i)->first)="storm";
22 C:\Documents and Settings\St0rM\Desktop\test.c incompatible types in assignment
why so ?
-
can't do that, use strcpy()
-
also
1. int main should be used
2. fix indentation
3. pass array size together with array pointer - don't use "magic numbers" like 4 in the get_struct function
4. x[i].first is a lot easier to read than ((x+i)->first)
-
works prefect
Code:
#include<stdio.h>
#include<string.h>
struct list
{
char first[20];
char name[20];
char blahblah[40];
};
void get_struct(struct list * x);
main()
{
struct list data[4];
struct list *ptr;
ptr=data;
get_struct(ptr);
}
void get_struct(struct list *x)
{
int i;
for(i=0;i<4;i++)
{
strcpy((x+i)->first,"storm");
strcpy((x+i)->name," man");
strcpy((x+i)->blahblah,"Is nothing But A good Lie");
printf("\nfisrt=%s",(x+i)->first);
printf("\nname=%s",(x+i)->name);
printf("\nlif is %s",(x+i)->blahblah);
strcat((x+i)->first,(x+i)->name);
printf("\nme =%s\n",(x+i)->first);
}
printf("Good Night");
}
but why i can't do
Code:
((x+i)->first)="storm";
?
isn't x+i-> access the first array ?
thanks again
-
you can access the array with (x+i)-> operator (although I think x[i]. looks nices)
but you cannot assigh to array in C...
To put contents in the char-array - you should use string-manipulation functions like strcpy
-
1. int main should be used
2. fix indentation
3. pass array size together with array pointer - don't use "magic numbers" like 4 in the get_struct function
4. x[i].first is a lot easier to read than ((x+i)->first)
ahhaaaaaaa
thanks for this notices 4. x[i].first is a lot easier to read than ((x+i)->first)
specially this 4. x[i].first is a lot easier to read than ((x+i)->first)
but you cannot assigh to array in C...
so i can only access it's content's but i can't assign any thing for it except with string manipulations functions :)
thanks for the tips
-
When a char array is first declared, you can THEN add a string of char's:
char_array[] = "It's a pirate's life for me"
After the initial declaration of the char array, you can alter it char by char:
char_array[i] = 'P'
if you like.
You can use all kinds of equivalent pointer notation with arrays, but they quickly become a PITA to debug, etc. imo