-1
#include<stdio.h>
#define msize 4096

struct memory
{
    int a[msize];
};

void main()
{
    struct memory m;
    m.a[0]=250; // temperature value of 25,0
    m.a[4]=01; // heater status OFF
    m.a[8]=240; // temperature value of 24,0
    m.a[12]=00; // heater status ON
    m.a[16]=220; // temperature value of 22,0
    m.a[20]=00; // heater status ON
    read(&m);

}

void read(struct memory m)
{
    int i;
    for(i=0;i<sizeof(msize);i++)
    {
        scanf("%d", m.a[i]);
    }
}

My code creates a structure of size 4096 bytes, an object for the structure and then assigns values to i.

On compilation, the compiler throws a "first defined here" error in the read function.

Also, can someone please help me to convert this read value to ASCII?

Anish Ramaswamy
  • 2,326
  • 3
  • 32
  • 63
user2306769
  • 11
  • 2
  • 4
  • 1
    When there's an error you should show it in the question - the compiler is trying to help. – sje397 May 08 '13 at 09:19
  • 5
    `sizeof(msize) != 4096` and `sizeof(struct memory) != 4096` and `void read(struct memory *m)` //read arg pointer – BLUEPIXY May 08 '13 at 09:23

3 Answers3

3

You need to pass scanf an address to write to so need to change

scanf("%d", m.a[i]);

to

scanf("%d", &m.a[i]);
//          ^

You should also consider passing a pointer to m to read rather than passing this huge struct by value

void read(struct memory* m)
{
    int i;
    for(i=0;i<msize;i++)
    {
        scanf("%d", &m->a[i]);
    }
}

(In fact your read(&m) call in main appears to already by assuming this update.)

simonc
  • 41,632
  • 12
  • 85
  • 103
  • I changed as above Then also its throwing an error within void read() as multiple definition of read and first defined here. – user2306769 May 08 '13 at 09:47
  • 1
    You need to either move `read` above `main` or provide a forward declaration above `main`. See the answer from sje397 for details. – simonc May 08 '13 at 09:55
1

In addition to what @simonc said, you should also declare the function up the top:

#include<stdio.h>
#define msize 4096

struct memory
{
int a[msize];
};

void read(struct memory m);

void main()
{
//...
sje397
  • 41,293
  • 8
  • 87
  • 103
  • I made changes as you said then also its throwing an error as multiple definition of read and first defined here in void read function(). – user2306769 May 08 '13 at 10:03
  • If you paste the exact text of the error that would help a lot. Try changing your function name - `read` is common. – sje397 May 09 '13 at 09:53
0
#include<stdio.h>
#define msize 4096
struct memory
{
int a[msize];
};

void read(struct memory *m)
{
int i;
for(i=0;i<sizeof(msize);i++)
 {
    scanf("%d",&m->a[i]);
 }
}

int main()
{
struct memory m;
m.a[0]=250; // temperature value of 25,0
m.a[4]=01; // heater status OFF
m.a[8]=240; // temperature value of 24,0
m.a[12]=00; // heater status ON
m.a[16]=220; // temperature value of 22,0
m.a[20]=00; // heater status ON
read(&m);
return 0;
}
Coffee_lover
  • 545
  • 6
  • 15
  • 1
    1. return type of main cant be void 2. read should have been placed before main 3.as @simonic already said use scanf("%d", &m->a[i]) it will compile fine without any error – Coffee_lover May 08 '13 at 10:14
  • thank you very much!!! could you give me some ideas to convert this values to ASCII ?? – user2306769 May 08 '13 at 10:33