• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Basic C programming help

HAL9000

Lifer
Can anyone explain to me how to do the following:

I have this code
Code:
typedef struct name{
some irrelevant crap}

name nam[10][15];

How do I get it to print out 15 or 10? Or whatever values are in those spots.

Basic, badly explained but I'm tired and stressed, anyone?
 
Last edited:
You have to go through each element of the struct that you want to print out and do it in whatever fashion you need.

For example (and pardon my C++ code if it sneaks in, I'll try and keep things close to C, but c++ still might slip in).

Lets say your stuct looked like this.

Code:
struct myStruct
{
   float a;
   int b;
   int d;
}

To display the data in the struct, you would do something like this
Code:
void displayStruct(myStruct* thing)
{
   printf("a: %f, b: %d, c: %d\n", thing->a, thing->b, thing->c);
}

if you had an array of structs, then you would just loop through each and print out their data. So something like this

Code:
for (int i = 0; i < numStructs; ++i)
{
   displayStruct(&structArray[i]);
}

A 2d array is the same thing but with more loops.
 
You have to go through each element of the struct that you want to print out and do it in whatever fashion you need.

For example (and pardon my C++ code if it sneaks in, I'll try and keep things close to C, but c++ still might slip in).

Lets say your stuct looked like this.

Code:
struct myStruct
{
   float a;
   int b;
   int d;
}

To display the data in the struct, you would do something like this
Code:
void displayStruct(myStruct* thing)
{
   printf("a: %f, b: %d, c: %d\n", thing->a, thing->b, thing->c);
}

if you had an array of structs, then you would just loop through each and print out their data. So something like this

Code:
for (int i = 0; i < numStructs; ++i)
{
   displayStruct(&structArray[i]);
}

A 2d array is the same thing but with more loops.

That's very helpful thankyou. 🙂
 
Can anyone explain to me how to do the following:

I have this code
Code:
typedef struct name{
some irrelevant crap}

name nam[10][15];

How do I get it to print out 15 or 10? Or whatever values are in those spots.

Basic, badly explained but I'm tired and stressed, anyone?

sizeof(name) gives you the # of bytes allocated per instance of name.

sizeof(nam) gives you the total bytes allocated for the array nam.
 
If your code is going to rely on the size of your array dimensions, I'd use #defines to define the dimensions.

I'm always paranoid that using sizeof(variablename) is going to include some padding bytes which might throw your logic off.
 
Back
Top