LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 09-05-2005, 11:08 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Rep: Reputation: 45
Unhappy extern static variable


Hi i have a problem using the extern definition with a struct
The struct is defined as following

from file diffServLinux.h
struct diffstat{
unsigned long int ifindex;
unsigned long int ifdirection;
char name[20];
unsigned long int level;
unsigned long int handle_major;
unsigned long int handle_minor;
..........
};

File monitor.c /*Strores values in the variables */
#include "diffServLinux.h"
struct diffstat ds_stats[10];
ds_stats[1].ifindex=20;

File logical.c /*Wants to see the values in the ds_stats struct */
I have tried using the extern statement
extern struct diffstat ds_stats[10];

I compile it using
gcc -o programme monitor.c logical.c
but this returns the error
logical.c: At top level:
logical.c:2: error: storage size of `ds_stats' isn't known

The compiler says that perhaps cant understand the internal structure of the ds_stats
I have tried also to include the diffServLinux.h in the logical.c
but then the compiler starts complaining about a bunch of redefinitions

Dont know what to do now... Plz help
 
Old 09-05-2005, 11:25 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
diffServLinux.h:
Code:
#ifndef DIFFSERVLINUX_H
#define DIFFSERVLINUX_H

struct diffstat
{
unsigned long int ifindex;
unsigned long int ifdirection;
char name[20];
unsigned long int level;
unsigned long int handle_major;
unsigned long int handle_minor;
};

#endif /* #ifndef DIFFSERVLINUX_H */
globals.h:
Code:
#ifndef GLOBALS_H
#define GLOBALS_H

#include "diffServLinux.h"

extern struct diffstat foo[10];

#endif /* #ifndef GLOBALS_H */
globals.c:
Code:
#include "globals.h"

struct diffstat foo[10];
main.c
Code:
#include <string.h> /* memset() */
#include <stdio.h>
#include "globals.h"

static
void bar(void)
{
   foo[1].level = 4711;
}

int
main(void)
{
   memset(foo, 0, sizeof(foo));
   
   bar();

   printf("%li\n", foo[1].level);

   return 0;
}
Makefile
Code:
CC = gcc
CFLAGS = -Wall -W -ansi -pedantic -g -O0 -c -o
LDFLAGS = -o $(EXEC)
EXEC = globalstructarraytest.exe
OBJECTS = globals.o main.o

all: $(OBJECTS)
	$(CC) $^ $(LDFLAGS)

%.o: %.c
	$(CC) $(CFLAGS) $@ $<

clean:
	rm -f $(OBJECTS) $(EXEC) *~ *.stackdump
Now there's a complete example on how you declare a global array of some struct where each struct (each element) can be changed during runtime and all translation units (read: files) that includes the header file globals.h will see the same array and not get its own instance of it.

Last edited by Hivemind; 09-05-2005 at 11:26 AM.
 
Old 09-05-2005, 11:28 AM   #3
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
Thx a lot cool
THHHXXX
 
Old 09-05-2005, 11:28 AM   #4
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
1 more question.. Where have u learned all this stuff?
 
Old 09-05-2005, 11:54 AM   #5
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Reading a lot of books (not web tutorials, but good books), asking on/reading the programming groups (comp.lang.c, comp.lang.c++ mostly, but also groups dealing with a specific platform) on usenet. Later in life I've also taken courses in programming at the university. I started with C++ ten years ago, but the first five years or so I had long periods when I did no programming at all (unfortunately).
 
Old 09-05-2005, 12:43 PM   #6
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
What do u mean by programming groups....
I am looking for a place that i can ask about c... Sometimes i think that this place isnt the most appropriate ... (linux forums)
 
Old 09-05-2005, 01:10 PM   #7
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Well, it seems you need to read up on what usenet is. If you google for usenet you should find a lot of information. Many ISP:s (Internet Service Providers) have a usenet server. You can configure most mail clients to allow you to read (and post on) usenet groups if your ISP has a news server. There are also programs that are exclusively for reading newsgroups. If your ISP doesn't have a news server, google provides a http-based access to it that's free. Before, google groups lagged behind several hours so it was inconvenient to use, but that's not the case today.

Head over to: http://groups.google.com/group/comp.lang.c


Keep in mind, though, that comp.lang.c only deals with the C language. Anything that's platform specific is off-topic for the group. But there are groups for developing on linux and other operating systems. If your question is pure C (like the one in this thread), ask in comp.lang.c, if it concerns libraries, api:s etc that's not part of the core language but provided by a specific operating system, ask in a group/forum dedicated to developing on that platform.

Last edited by Hivemind; 09-05-2005 at 01:13 PM.
 
Old 09-05-2005, 02:30 PM   #8
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

In addition to the Stevens book, I'd also get a copy of Kernighan and Ritchie. It's the single best reference for these kinds of questions in the entire world. It's also one of the best "computer books" ever written. No kidding! And you can probably buy a copy on Amazon for around $20.00:

Kernighan & Ritchie: The C Programming Language:
http://www.amazon.com/exec/obidos/tg...=UTF8&v=glance

Unix Network Programming, 3rd Edition:
http://www.amazon.com/exec/obidos/tg...glance&s=books
 
Old 09-10-2005, 08:10 PM   #9
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
Thx hivemind about your second reply... I have subscribed to google groups for c language.. but still i cant find any subgroup for linux c ...
 
Old 09-11-2005, 05:11 AM   #10
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Maybe comp.unix.programmer (many API:s and libraries work the same across unix and linux) and comp.os.linux.development.apps? I'm sure there are others. linuxquestions is fine for platform specific questions, but if you really need help from C experts comp.lang.c is the best.
 
  


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
Howto Assign Multiple Static Public IP Addresses under SBC's PPPoE Static Ip system o trekgraham Linux - Networking 8 04-17-2007 11:51 AM
What is a static variable? saiz66 Programming 7 09-30-2004 06:17 PM
FC2 Overriding static if in favor of dhcp system set for static pkraus109 Linux - Networking 8 09-21-2004 12:13 PM
extern "C" and static data type problem with g++ vtluu Red Hat 1 05-21-2004 11:45 AM
extern "C" and static data type problem with g++ vtluu Programming 2 04-28-2004 06:10 AM


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

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration