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

multiple storage classes in declaration specifiers

When I try to compile these two following files I always get the error message
file1.c: In function ‘main’:
file1.c:8: error: multiple storage classes in declaration specifiers
Could anyone please help me in this regards

Expand|Select|Wrap|Line Numbers
  1. .h
  2. #define NDATA 5
  3.         float r[NDATA];
  4.  
file1.c
Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2. #include "dataread.h"
  3.  
  4. extern float r[NDATA]; 
  5.  
  6. int main()
  7. {
  8.         extern void static data(float r[NDATA]);  
  9.  
  10.         data(r);
  11.         int i;
  12.  
  13.         for (i = 0; i<NDATA ; i++)
  14.         {   
  15.                 printf("%f \n", r[i]);
  16.         }   
  17. }
  18.  
file2.c
Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2. #include "dataread.h"
  3.  
  4. extern float r[NDATA];
  5.  
  6. void static data(float r[NDATA])  
  7.  
  8. {
  9.         FILE *fo;
  10.         int i;
  11.  
  12.         fo = fopen("1.txt", "r");
  13.  
  14.  
  15.         /* Read data */
  16.         for (i = 0; i<NDATA; i++)
  17.         {   
  18.                 fscanf(fo, "%f", &r[i]);    
  19.                 printf(" read line %d %f\n", i, r[i]); 
  20.         }   
  21.  
  22.         return;
  23.  
  24. }
Oct 6 '11 #1
8 35820
weaknessforcats
9,208 Expert Mod 8TB
You say:

Expand|Select|Wrap|Line Numbers
  1. extern void static data(float r[NDATA]); 
  2.  
The static storage class declares the variable local to the file whereas the extern storage class declares the variable either external to the file or internal to the file but with external linkage.

Big conflict here so the compiler generates an error.
Oct 6 '11 #2
Please tell me how can I solve this problem. I want to make the array r[NDATA] global, so that I can use it in file1.c.
Oct 6 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
It looks like you want a global array:

Expand|Select|Wrap|Line Numbers
  1. #include"test.h"
  2.  
  3. int main()
  4. {
  5.   float* ptr = data();  //get address of array
  6. }
  7.  
So you call the data() function to get the address of the array.

Next in another .c file, you define the array as a static array. This makes access to the array local to the functions in the .c file where the array was defined. In this file you define the data function, which when called, returns the address of the array. Since the array is static, access form other files has to go through the data() function:

Expand|Select|Wrap|Line Numbers
  1. static float r[NDATA];
  2.  
  3. float* data()
  4. {
  5.      return r;
  6. }
  7.  
Lastly, write a header with the prototype of the data() function and include this header in all of yur other source files:

Expand|Select|Wrap|Line Numbers
  1. float* data();
As you move forward add functions to add/retreive data elements, etc. Only the functions in the file where the array is defined need the NDATA constant.
Oct 6 '11 #4
file1.c
Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2. #include "dataread.h"
  3.  
  4. float *data(); 
  5.  
  6. int main()
  7. {
  8.         float *ptr = data(); 
  9.         int i;
  10.  
  11.         for (i = 0; i<NDATA; i++)
  12.         {   
  13.                 printf("%f \n",*ptr); 
  14.         }   
  15. }
  16.  
  17.  
  18. #include "stdio.h"
  19. #include "dataread.h"
  20.  
  21. static float r[NDATA];  
  22.  
  23. float *data()
  24. {
  25.         FILE *fo;
  26.         int i;
  27.  
  28.         fo = fopen("1.txt", "r");
  29.  
  30.  
  31.         /* Read data */
  32.         for (i = 0; i<NDATA; i++)
  33.         {   
  34.                 fscanf(fo, "%f", &r[i]);    
  35.         }   
  36.  
  37.         return r;
  38.  
  39. }
.h
Expand|Select|Wrap|Line Numbers
  1. #define NDATA 5 
  2.  
  3. float *data();
I tried as above.
But I get first data only NDATA times from file1.c. Please tell me where I am doing wrong.
Oct 7 '11 #5
weaknessforcats
9,208 Expert Mod 8TB
This code in main():

Expand|Select|Wrap|Line Numbers
  1. for (i = 0; i<NDATA; i++)
  2.  { 
  3. printf("%f \n",*ptr); 
just displays ptr[0] NDATA times. Use *(ptr+ i) or ptr[i] instead.
Oct 7 '11 #6
Thank you. It works. However, when I tried this for a two column datafile in the follwing way:

1. In file2.c: Read data file, split columns and store each column in two files, define two function (float *grav_rad() and float *grav_dens()) for each array(column).

2. Call the function from file1.c and print the arrays

The compilation does not show any error, but when I run it I get the message: Segmentation fault

Please tell me where I am wrong and how can I fix the problem.

file1.c
1 #include "stdio.h"
2 #include "dataread.h"
3
4 float *grav_rad();
5 float *grav_dens();
6
7 int main()
8 {
9 float *ptr = grav_rad();
10 float *ptrho = grav_dens();
11 int i;
12
13 for (i = 0; i<NDATA ; i++)
14 {
15 printf("radius: %f \n",*(ptr+i));
16 printf("density: %f \n",*(ptrho+i));
17 }
18 }

file2.c
1 #include "stdio.h"
2 #include "dataread.h"
3
4 static float r[NDATA];
5 static float rho[NDATA];
6
7 float *grav_rad();
8 float *grav_dens();
9
10 void dataread()
11 {
12 FILE *fo, *fr, *frho;
13 int i;
14
15 fo = fopen("2.txt","r");
16
17 /* Read data */
18 for(i = 0; i < NDATA; i++)
19 {
20 fscanf(fo, "%f %f", &r[i], &rho[i]);
21 printf("%f %f\n", r[i], rho[i]);
22 }
23
24 fr = fopen("fr.txt", "w");
25 frho = fopen("frho.txt", "w");
26
27 for(i = 0; i < NDATA; i++)
28 {
29 fprintf(fr, "%f \n", r[i]);
30 fprintf(frho, "%f \n", rho[i]);
31 }
32 }
33
34 float *grav_rad()
35 {
36 FILE *fr;
37 int i;
38
39 fr = fopen("fr.txt","r");
40
41 for(i = 0; i < NDATA; i++)
42 {
43 fscanf(fr, "%f", &r[i]);
44 }
45
46 return r;
47 }
48
49 float *grav_dens()
50 {
51 FILE *frho;
52 int i;
53
54 frho = fopen("frho.txt","r");
55
56 for(i = 0; i < NDATA; i++)
57 {
58 fscanf(frho, "%f", &rho[i]);
59 }
60
61 return rho;
62 }

.h
1 #define NDATA 5
2
3 float *grav_rad();
4 float *grav_dens();
Oct 8 '11 #7
weaknessforcats
9,208 Expert Mod 8TB
It looks like you are reading and writing floats to files. However, fscanf needs a delimiter so when you write to the file you need to separate your float values by a space.

A segmentation fault is always a memory corruption that you caused.

Also, there is no error checking in this code so you never know if the file opened or was read or written correctly.

Next, learn to use your debugger and step through this code a line at at time. The error will leap right out at you. This is better then putting in printf statements.
Oct 8 '11 #8
Another good resource for Storage Classes
Dec 25 '14 #9

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

Similar topics

0
by: Hai NIng | last post by:
Does any one know how to implement a multiple-storage clipboard, sort of like the clipboard in office where it can hold multiple number of text/image/custom data, etc.? Thanks. - Harry
1
by: Daniel Schüle | last post by:
Hello all, I get the error when I try to compile this code // .h file typedef static int (*get)(void); // (*) typedef static void (*set)(int); // // .c file
8
by: Tapeesh | last post by:
I have a following piece of code. The code was compiled using g++ class A { public : virtual void fn() = 0; }; class B: virtual private A {
6
by: cp | last post by:
I'm not an excellent C-programmer so forgive possible, in-your-eyes, stupid mistakes below. struct book { char name; char genre; int serial_number; } struct book * insert_book(char...
1
by: Geevi | last post by:
Hi all, Explain about automatic and static storage classes? How can they bind with external linkage and internal linkage? Differentiate between scope and life teime of the variable?
15
by: iKiLL | last post by:
hi all, I would like to be able to create an umbrella class for all my main global sections but I would still like to keep them all in separate file something like the below but I keep getting...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
1
by: Freelander | last post by:
Hi group, Just joined the group and I'm after some help as the title might suggest. Using Open Watcom for writing a program but need hellp with the following:- struct packet { Ecbtype ...
4
by: patelss23 | last post by:
Hello All, how are you? Can you please make me aware of storage specifiers like volatile, extern, and register ? I know the in what situations they can be used. But till this date I haven't seen...
2
by: sumsin | last post by:
are 'mutable' and 'volatile' a Storage Classes in C++?
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
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: 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)...
1
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.