473,408 Members | 2,402 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,408 software developers and data experts.

How to get a structure variable in function?

Can I get a structure variable in function
for eg:-
Expand|Select|Wrap|Line Numbers
  1. struct stud{ 
  2.             char name[20]}stud1;
  3.  
  4. void student(char);
  5. void main()
  6. {
  7.     char n[20];
  8.     scanf("%s",n);
  9.     strcpy(stud1.name,n);
  10.     student(stud1.name);
  11. }
  12. void student(char k)
  13. {
  14.    printf(k);
  15. }
Nov 3 '10 #1
2 2737
Banfa
9,065 Expert Mod 8TB
Do you mean can you pass it to a function? Yes.

The question you need to ask is what is the type of stud1.name. That is nothing to do with structures but rather is a question about arrays.

Once you can answer that question then you just need to give your function parameter that type.

As a clue you already pass stud1.name to another function at line 9. What type does that function use to accept it?
Nov 3 '10 #2
Oralloy
988 Expert 512MB
Sajini Biju,

You have several couple choices, depending on what you need to accomplish.

You can pass just the address of the name array:
Expand|Select|Wrap|Line Numbers
  1. void student(char *);
  2. void main()
  3.     char n[20]; 
  4.     scanf("%s",n); 
  5.     strcpy(stud1.name,n); 
  6.     student(stud1.name); 
  7. void student(char *k) 
  8.    printf("%s", k); 
  9. }

You can pass the entire stud structure:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. void student(struct stud);
  3. void main()
  4.     char n[20]; 
  5.     scanf("%s",n); 
  6.     strcpy(stud1.name,n); 
  7.     student(stud1); 
  8. void student(struct stud k) 
  9.    printf("%s", k.name);
  10. }



You can pass a reference to the stud structure:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. void student(struct *stud);
  3. void main()
  4.     char n[20]; 
  5.     scanf("%s",n); 
  6.     strcpy(stud1.name,n); 
  7.     student(&stud1); 
  8. void student(struct stud *k) 
  9.    printf("%s", k->name);
  10. }

Note, too, that I inserted a format in your printf() call. This is to guard against the case where the string to be printed has something that appears as a format specification, which would then result in garbage out.

Luck!
Nov 3 '10 #3

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

Similar topics

4
by: Zoran Stipanicev | last post by:
Hi! I have a code that looks some thing like this void function() { struct st1 { template<...> struct st2 {
2
by: rashmi | last post by:
Hi All, ************************************************* error: variable `hdlc_cdevsw' has initializer but incomplete type *************************************************** This is the error...
5
by: Gary Wessle | last post by:
Hi I have a group of functions which have the same signature. void fun_n(void); according to a conditional structure "be it if-else or switch-case" I get to choose which one to run. ...
3
by: not_a_commie | last post by:
Does MS have an official C# variable/function/codingstyle standard that is available on the internet? What standard do they use for their internal coding? Mind posting a link? Thanks.
4
by: Abdul Samad | last post by:
banfa just said that it is because so the compiler may not assign the same memory to two variables but my question is WHY DOES COPILER ASSIGNMS MEMORY WHEN THERE IS NO NEED OF IT? as the...
5
by: lovecreatesbea... | last post by:
Does the expression *(int *)&s1 below inside the printf() statement guarantee to refer to the first member of the structure variable s1? I've tried the code and it seems that it works that way. The...
4
by: koffee | last post by:
Hi people, I've got a question on global structure initialization. A global variable like int can be explicitly initialized as: int Global = 1; But how can I explicitly initialize a...
3
by: =?Utf-8?B?TWluZ3lp?= | last post by:
Hi, I have the following question regarding communicating with a OCX control using C#. Idon't have access to the ocx code, so my c# code is actually mimic the behavior of C++ counterparts....
8
by: Roy Strachan | last post by:
I have seen this before and know it works (at least on an old Borland compiler). Unfortunately, though my memory is good, it's short. Using gcc how do I (can I?) initialize TESTSTRUCT.ssize to ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.