LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-24-2005, 01:00 PM   #1
ngan_yine
Member
 
Registered: Aug 2003
Distribution: Slackware 10.1
Posts: 113

Rep: Reputation: 15
pascal to C problem in struct storing data


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,};

please tell me why can't we use in first way?
 
Old 01-24-2005, 02:25 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.

Last edited by itsme86; 01-24-2005 at 02:28 PM.
 
Old 01-24-2005, 02:45 PM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
I personally always feel safer initializing all strings with strncpy.

Code:
#include <string.h>

struct mailing {
        char name[100];
        int phone;
};      

int main() {
        struct mailing *list = malloc (sizeof(struct mailing));
        
        strncpy(list->name,"ngan yine",100);
        list->phone = 1234466;
        
        /* lots more code...*/
        free(list);
        return 0;
}
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.
 
Old 01-24-2005, 03:52 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Of course, then you always have the potential of having a non-terminated string:
Code:
#include <string.h>

int main(void)
{
  char small[5];

  strncpy(small, "too big", sizeof(small));
  // Uh oh! small contains 't' 'o' 'o' ' ' 'b'. No '\0'!

  return 0;
}
Of course adding a small[sizeof(small)-1] = '\0'; after every strncpy() call will keep you safe.
 
Old 01-24-2005, 09:45 PM   #5
ngan_yine
Member
 
Registered: Aug 2003
Distribution: Slackware 10.1
Posts: 113

Original Poster
Rep: Reputation: 15
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?
 
Old 01-25-2005, 01:48 AM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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').
 
Old 01-25-2005, 03:21 AM   #7
ngan_yine
Member
 
Registered: Aug 2003
Distribution: Slackware 10.1
Posts: 113

Original Poster
Rep: Reputation: 15
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 .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
Problem with Java and Pascal usr Programming 6 05-19-2004 01:32 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM


All times are GMT -5. The time now is 09:21 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration