423,321 Members | 1,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,321 IT Pros & Developers. It's quick & easy.

multiple storage classes in declaration specifiers

P: 4
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
Share this Question
Share on Google+
8 Replies


weaknessforcats
Expert Mod 5K+
P: 9,163
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

P: 4
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
Expert Mod 5K+
P: 9,163
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

P: 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
Expert Mod 5K+
P: 9,163
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

P: 4
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
Expert Mod 5K+
P: 9,163
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

P: 3
Another good resource for Storage Classes
Dec 25 '14 #9

Post your reply

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