LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 02-17-2012, 03:47 PM   #1
zeelog
Member
 
Registered: Jan 2008
Posts: 139

Rep: Reputation: 1
C language do structures always have to be defined globally ?


All the C language examples I see have structures defined
globally. I have read global variables are to be avoided
and would like to define my structures within a function or
within main(). Can this be done ?
How can I do this when a function prototype must be defined
with a pointer to a structure ? Function prototypes must
be defined globally, right ? So how can I define this
prototype when the structure is NOT defined globally ?
#include <stdio.h>
struct tag { char first[40];
char second[40];
int speed; };
struct tag butter;
void show_butter( struct tag *b ); /* display butter */
int main(void)
{
------ I want to define and declare the structure here
or inside a function yet to be written -------
struct tag *ptr;
ptr = & butter;
----- more code loading structure with data ---
show_butter( ptr );
return 0;
}
I hope I got the idea across and my code isn't to sloppy.
Any ideas would be welcome. I'm using gnu C in Slackware.
 
Old 02-17-2012, 04:12 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
perhaps you want:
Code:
typedef struct { ... } bah;
This says that bah is now a type-name that refers to a particular struct. That is to say: "I, the programmer, do hereby inform you, the computer, that, by virtue of mine own imperial decree, a variable of type bah is a structured type whose component elements forsooth shall be as follows ..."

The syntax of "C" was envisioned somewhat for convenience as opposed to rigor. Messers. Kernighan and Ritchie made some decisions that just seemed good to them at the time. (They knew a great deal about language design, of course, but nevertheless, there does come a point in the design process where you have to toss that coin.)

This typedef syntax is, I think, quite suitable, because you can now say things like: bah *thisbah = &thatbah, where you have elsewhere also defined thatbah to be of the same type, bah.

Last edited by sundialsvcs; 02-17-2012 at 04:16 PM.
 
Old 02-17-2012, 04:14 PM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by zeelog View Post
All the C language examples I see have structures defined
globally. I have read global variables are to be avoided
and would like to define my structures within a function or
within main(). Can this be done ?
Yes, it can be done. You can define a struct inside a function. You can then only use that struct inside that function.
But by defining it globally, you are not creating a global instance of it. Except for keeping your global namespace clean by not creating definitions of something you only use in a limited scope, there is no real benefit I know of of defining structs locally.

Quote:
Originally Posted by zeelog View Post
How can I do this when a function prototype must be defined
with a pointer to a structure ? Function prototypes must
be defined globally, right ? So how can I define this
prototype when the structure is NOT defined globally ?
As I said, if you define structure locally, you can only use it in the function in which it is defined, so unless you are defining the function inside the function where the struct is defined (which in C is not possible), you can not use that struct as an argument or return type of any function. If you want to do that, declare the struct globally.
 
Old 02-17-2012, 04:22 PM   #4
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Defining a struct itself is usually done globally because there is usually no need to specifically confine the definition to a particular scope. But this is the definition of the struct itself, NOT the definition of a variable having the type of that struct, and using memory space.

A definition that takes up space can be defined globally. Unfortunately, too many programmers do define things globally out of laziness or other reasons, when there is otherwise no technical reason to do so. But sometimes there are good reasons for it. I just often see it done when there are not any good reasons for it.

What you should do is just define the "shape" of the structure globally, like:
Code:
struct my_node {
    struct my_node *next;
    struct my_node *prev;
    double number;
} /* do not put a variable name here */;
If you are making a project in multiple files, this should be placed in a header that all the files needing it will include (so it is defined in one common place to avoid the definition in multiple places getting out of sync).

Then you define the function as returning a pointer to this struct, or whatever. And then you define variables which are actual data of this struct type, or pointers to such, in various places (in main() or in any function).

Do not define space with the "shape definition". Define variables (or functions) using it in the scope they need to be in.
 
Old 02-17-2012, 04:32 PM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Sorry if off-topic.

Quote:
Originally Posted by millgates View Post
As I said, if you define structure locally, you can only use it in the function in which it is defined, so unless you are defining the function inside the function where the struct is defined (which in C is not possible), you can not use that struct as an argument or return type of any function. If you want to do that, declare the struct globally.
About nested functions in C: link.

To my knowledge, defining local structures and/or local (nested) functions is not the preferred way of programming in C (as opposed to, say, scheme or ruby). You can make a function local to a compilation unit by defining is as static. Such a function is not visible outside of the file it is defined in. Static functions has some other advantages.

Last edited by firstfire; 02-17-2012 at 04:34 PM.
 
Old 02-18-2012, 01:02 PM   #6
Peverel
Member
 
Registered: May 2009
Location: Chelmsford, England
Distribution: OpenSuse 12.2 and 13.2, Leap 4.2
Posts: 128

Rep: Reputation: 24
Well, of course all variables can be local or global; structs are no different from any other. But I'm a bit worried by the initial premise here:
Quote:
I have read global variables are to be avoided.
If a variable is to be accessed by more than one function, then it must be global (except for Gnu C, which allows non-local variables, including functions, but that's another story).
A variable should be local if it is a scratch variable, used only in one function, say for calculation, and not needed after the function closes (if it is needed with the same value in a later call of the same function it can indeed be static, but here the variable value, but not its name, is stored with the global variables). Otherwise, if functions p() and q() both use a global variable a for internal calculation and we have code like
Code:
void p()
{
  ...
  a=7;
  q();
  ...
}
then the call of q() may alter a and upset the remaining code in p(). Defining a locally in both p() and q() avoids this. They are different variables with the same name, but that name having restricted scope. If p() and q() were parts of a library, then there might be different programmers writing them, which makes it all the more important.

The other case where local variables must be used is in functions which use recursion (including multiple recursion), because here we will have several calls of a function open at the same time in which the same variable will have different values in different calls (but they must not be static, because there is only one copy of a static variable).

Last edited by Peverel; 02-18-2012 at 01:08 PM.
 
  


Reply



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
where defined(__i386) is being defined?? syseeker Programming 1 06-27-2006 06:24 AM
Circularly defined structures in C++ infinity42 Programming 2 10-30-2005 12:03 PM
How to install Firefox 9.1 Globally? rignes Linux - Software 8 07-25-2004 12:36 AM
Changing my keyboard layout (language) globally by command (shell) Santorres Linux - Newbie 1 05-17-2004 02:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:16 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration