473,388 Members | 1,426 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,388 software developers and data experts.

Struct in C

3
Please help me. I am new to C and working on a zilog micro. I need to read all elements of a structure I have set up and write theses bytes to EEPROM.
I thought the way was to increment a pointer to the structure, but seem not to have much success doing this. Please advise how to go about this.

My code is:

struct Calib // Structure to hold calibration data
{
int DcOffset[3]; // Array [x1,x10,x100]
signed int CorrFctr[2][3][3]; // Array [V,I] [Red,White,Blue] [x1,x10,x100]
} CalData;

struct Calib *st_ptr;

for (i = 0; i <= 80; i++)
{
printf("Value at this address is: %d \n", st_ptr);
*st_ptr++;
}

Also, Please advise if one can assign constants in a struct. For example, can I add the element below, to the structure:

unsigned int CalVal[2][3] = {{230, 230, 230}, {25, 5, 1}};
Nov 1 '06 #1
2 1897
What do you exaclty want to know. Can you post the hypthetical output you expect.

You code will print the pointer(addresses) of adjacent structures. thus if we say (suppose) the junk address contained by defn:

Expand|Select|Wrap|Line Numbers
  1. struct Calib *st_ptr;
is 4022 and size of int(or unsigned int) on your machine is 4, then delta=(3+3*3*2)*4 = 84, your program will print
4022
4106
4190
......
......
with a gap of sizeof( struct Calib ) = 84 everytime ..81 times. Why do you want to print this?
Nov 2 '06 #2
Toe007
3
Thank you for the answer - I actually reposted this question under the heading "Reading contents of a structure" as I could not find my original posting at first.

To answer your question, I am wanting to read out all the bytes of a structure and to write these to EEPROM i.e map the memory occupied by the contents of the structure to EEPROM. This is part of a uP development I am doing.

So what I am trying to figure out (in my newness to C), is if there is an elegant way to access the contents of the structure one byte at a time, using a pointer which I increment. I picture this a bit like starting the pointer at the first element in an Array[x] and reading a byte each time the ptr is incremented. only problem is I do not know how to do it with a structure containing multiple members.

I need something along the lines of :

void TestRoutine (void)
{
struct Calib // Structure to hold calibration data
{
signed int DcOffset[3];
signed int CorrFctr[2][3][3];
} CalData;

unsigned int i;
struct Calib *st_ptr;

st_ptr = &CalData;

for (i=0; i <= SizeOfStructure, i++) do
{
printf("Value at this address is: %d \n", *st_ptr); // write contents at address pointed to by pointer
st_ptr++; // point to next byte in structure and repeat
}



I know something like

for (i = 0; i <= 3; i++)
printf("Value at this address is: %d \n",st_ptr->DcOffset[i]);

will work, but seems to me there should be a way to read each byte from the structure Calib without having to reference its members such as DcOffset ?

Thanks.
Nov 3 '06 #3

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

Similar topics

5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
10
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
7
by: Alex | last post by:
If I have two struct. See below: struct s1 { int type; int (*destroy)(struct s1* p); } struct s2 { struct s1 base;
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
4
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.