473,320 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Filling char field in struct and array of a struct

19
Question 1: How can i get Name filled with the name steve without having to scan or fscan using C?

Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2.    {
  3.     char  name[40];
  4.    } PERSON;
  5.  
Question 2: How would I make an array (lets say of size 5) with each element being type PERSON. Once made how would I print out the name in element 3?
Jun 4 '08 #1
7 3590
If input comes from a stream (STDIN, file ... ) you can always fill it in with syscall read(), which is what actually scanf does.

See http://www.opengroup.org/onlinepubs/007908799/xsh/read.html

If the assignment is to be hardcoded, then you can use strcpy from string.h or directly assigning the chars, i.e.

Expand|Select|Wrap|Line Numbers
  1. PERSON personVar;
  2.  
  3. personVar.name[0]='S';
  4. personVar.name[1]='t";
  5. ...
  6. personVar.name[5] ='\0';
Jun 4 '08 #2
Your second question pretty much sound like your homework.
Jun 4 '08 #3
odin607
19
nah its not homework (its summer duh) i figured someone would say that though so heres the story behind it if you really wanna know~

both of these questions are from the same place...i realized i didnt know how to do either of them when i was helping a friend towards the end of the year with his first semester C class (im on my second, hence why i was like why dont i know this?)

but anyways so theres really no way to do "steve" directly then eh =/ (towards Q1)

and any answer on Q2? you can wait a week to answer if you really think its homework still doesnt bother me at all.
Jun 6 '08 #4
oler1s
671 Expert 512MB
but anyways so theres really no way to do "steve" directly then
Your question is confusing. What do you mean “directly”? If you don’t want to use formatted input (which is what the scanf family is) then your alternative is fgets (which is usually what you want to use anyway). If you seek to read input without using the standard library, then you’ll have to go through the OS specific APIs.

and any answer on Q2?
Consider for a moment, the question, of how would you make an array of size 5, of element type char. Clearly, it’s
Expand|Select|Wrap|Line Numbers
  1. char something[5]
Now look at the semantics of the structure definition you have there. You define a structure, with a member name. This structure defines a new datatype, and you give this datatype a new name (with typedef): PERSON. Now, swap out the element type char with element with PERSON. So you have:
Expand|Select|Wrap|Line Numbers
  1. PERSON something[5]
Once made how would I print out the name in element 3?
Printing out can be done with puts or printf. You want to pass in the array name, so you have something[2].name for the printf or puts argument.
Jun 6 '08 #5
odin607
19
hmm sorry for being vague there

since its kinda strange to explain ill give an example

same idea except with int (since obviously this works)

int user_id;
user_id=4;

is it possible to do something like this where the program inputs the value itself (in the example where it sets it = to 4).

and thanks on Q2, i got it to work.. i knew conceptually that it would be something like that, but it just didnt click till i read that
Jun 7 '08 #6
oler1s
671 Expert 512MB
Oh, so you want to do something like
Expand|Select|Wrap|Line Numbers
  1. name = “Steve”
  2.  
That won’t work. C strings aren’t true datatypes. They’re implemented as character arrays, so you have to treat them as arrays. If you have a C string and you want to put something like “Steve” in it, your best bet is to rely on strcpy. Anything else is going to be inefficient.
Jun 7 '08 #7
odin607
19
ya thanks strcpy is what i was looking for... forgot about it till i ran into it after like 3 hours of googling stuff

anywho thank yee again
Jun 7 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: rasika | last post by:
I have a structure typedef struct m { char *name, char **alais; }mystruct; The way I have initialised name field of the struct i want to intialise the alais field to { "geeta", NULL } char...
6
by: dddddddd2444444 | last post by:
Hi,please help... It works fine when I define a 2-D array like char code. But it won't work when I try to define the array dynamically using a function. It just crashes. Does anyone know why?...
7
by: JS | last post by:
I have this struct: int main() { struct jb { char actor; struct jb *next; }; struct jb *bond;
0
by: Kurt Ng | last post by:
Need help quick!!! Am really stuck on this problem! I have a C dll, and it uses a nested struct. (see below) The struct has 2 layers of nested structs. The first field in the first struct is...
3
by: MSDousti | last post by:
Hello I want to write a C# (or VB.NET) program which reads the structure of a PE (odinary win32 executable) file. These files have a long header (512 bytes or so). The definition of the header...
1
by: jurot | last post by:
Hi. I have struct in C++: struct MY_STRUCT { int x; int y; char** arrNames; //array of strings }
20
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
12
by: ab12 | last post by:
I'm trying to write a program in C that gets a shape outlined with asterisks from the user, and returns that shape filled with asterisks. It will also get the coordinates of a point inside the shape...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.