ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi
I used to program in pascal for my school and now I am learning c by myself ;
in pascal we would call it function and it like struct in C;
I want to know why can't we initilizalite the string in this way;
struct mailing{ /*first way*/
char name[100];
int phone;
}list;
int main(){
list.name="ngan yine";
but for integer we can;
list.phone=1234466;
in pascal we usually does it in similiar way .
Is there any way we can initilizlite without working around this way below;
struct mailing{
char name[100];/*second way*/
int phone;
} list = {
"ngan yine",
12323232,};
It has nothing to do with the struct. It has to do with arrays. You can't do this either:
Code:
int main(void)
{
char name[100];
name = "blah";
return 0;
}
But you can do:
Code:
int main(void)
{
char name[100] = "blah";
return 0;
}
If you want to do it your way then you can do this:
Code:
int main(void)
{
char *name;
name = "blah";
return 0;
}
Don't try to modify the contents of name after that though, because "blah" is stored in read-only memory. Otherwise you'll have to use strcpy() to store the string in the name variable.
strncpy lets you put bounds on how much to copy from the source to the destination. So if for some reason your source is bigger then your destination you won't end up with a segfault or bad data somewhere in your program.
Hi
Thanks guys ,That clear a few thing up;
I guess I still think as pascal programer because in pascal after declaring string as variable;
program myprogram;
var
name string;
began
name:="ngan yine";
end.
Declaring and initlizliting in such way is allow in pascal and I guess I was thinking in such way in C .I only found out that is wrong only when I tried to complie ,gcc tell me that there is incompartiable type in
certain line and I got lost untill you guy point me out;
I guess I forgot that in C the string is declare as array of char and not as string so bascially
name ="ngane yine" would be illegal to use;
But one more question ;is there any way to decare name as string in C?
like in pascal?
There is no such thing as a string like you're thinking of in C. In C, a string is defined as an array of characters terminated with an ASCII value 0 ('\0').
Ok!
Thanks for telling me that there is no string format in C.I thought all the hight level language
have string as in pascal because is such a important feature as integer or float(in pascal we call it real);I guess I have to stop thinking in pascal and start fresh new with C.
Thanks .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.