# Declare a variable and then use it inside a struct.

Page 1 of 1

## 13 Replies - 6004 Views - Last Post: 02 March 2012 - 12:10 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=269122&amp;s=6f0c0695f6ff9462ad65c88b02635d85&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 22
• Joined: 13-February 11

# Declare a variable and then use it inside a struct.

Posted 02 March 2012 - 09:41 AM

Moving through this book I have trying to learn C im at a point where it wants me to write code to practice using structs.

1) Delcare a standalone char array x[32]
2) make a struct called whatTheX which includes
- a length
- a width
- the variable declared in part 1)

The part i don't understand is how to redeclare x[32] inside the struct?

```char x[32];

struct whatTheX{
int length;
int width;
/* How do i use x[32] here?? */
}

```

Any help at all would be greatly appreciated =)

Is This A Good Question/Topic? 0

## Replies To: Declare a variable and then use it inside a struct.

### #2 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: Declare a variable and then use it inside a struct.

Posted 02 March 2012 - 09:49 AM

You would need to add a character array in the structure as well.
```char x[32];

struct whatTheX{
int length;
int width;
char structVar[32];
/* How do i use x[32] here?? */
};  // Don't forget this required semicolon.
```

The objective here seems to be that you learn how to access these structure members from your program. So you may want to create an actual program that you can experiment with.

Jim

Reputation: 0
• Posts: 22
• Joined: 13-February 11

## Re: Declare a variable and then use it inside a struct.

Posted 02 March 2012 - 09:55 AM

but wouldn't structVar just be an entirely new variable? When I modify it x wouldn't change would it?

### #4 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: Declare a variable and then use it inside a struct.

Posted 02 March 2012 - 10:08 AM

The following:

Quote

1) Delcare a standalone char array x[32]

Says standalone, standalone would not be part of your structure, it would stand alone. I would say that the purpose of this exercise will be for you to fill in the structure from these standalone variables that you create in your function.

Jim

This post has been edited by jimblumberg: 02 March 2012 - 10:28 AM

### #5 shurd

Reputation: 37
• Posts: 162
• Joined: 05-February 12

## Re: Declare a variable and then use it inside a struct.

Posted 02 March 2012 - 10:19 AM

You could have a char pointer and point it to X, if that's what you need.

Reputation: 0
• Posts: 22
• Joined: 13-February 11

## Re: Declare a variable and then use it inside a struct.

Posted 02 March 2012 - 10:29 AM

shurd, on 02 March 2012 - 10:19 AM, said:

You could have a char pointer and point it to X, if that's what you need.

I guess, but wouldn't that restrict me to only making 1 whatTheX because if I made more it would keep changing the x array? =S

I think i just need a new book This book shows simple examples and then asks practice questions that are way more complex and vague, and then doesn't give any answers.

### #7 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: Declare a variable and then use it inside a struct.

Posted 02 March 2012 - 10:35 AM

What book are you trying to use? You may want to post the complete question you are trying to understand. Without any context it is hard to guess what the book is trying to explain.

Jim

Reputation: 0
• Posts: 22
• Joined: 13-February 11

## Re: Declare a variable and then use it inside a struct.

Posted 02 March 2012 - 10:48 AM

i'm using System Programming With C and Unix.

here's the whole question:
------------------------------------------------------------------------
Create the C Code to store and use this collection of data:

1) A group of info. on an object
- Height
-Width

2) A standalone variable x[32] of type char

3) A more complex group of information
- The group from 1)
- Depth
- number of objects
- the variable from 2)

Use a nested structure construct for this solution. Incorporate this into a program, create the structures so they are declared inside the main().

create some test data to populate the fields of the struct, then a method that prints the values inside a single nested structure.
----------------------------------------------------------------

So the two issues I have with the question are:
1) Using the x[32] variable from #2 in #3
2) The book didn't teach me how to pass a struct to a function when the struct is only declared inside the main().

Everything was going so smoothly until i hit this chapter. Oh and anyone reading this, don't get this book if you plan on actually learning xD

### #9 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: Declare a variable and then use it inside a struct.

Posted 02 March 2012 - 11:03 AM

So this appears to be saying create one structure with two data members height and width.

```typedef struct
{
int height;
int width;
}Info;

// Then create another structure that contains that structure plus another variable a char x[32].

typedef struct
{
Info info;
char x[32];
}Combined;

```

I do disagree with the following statement:

Quote

Incorporate this into a program, create the structures so they are declared inside the main().

I think these structures should be created outside of main(), so you can pass these structures into your functions. If you create the structures inside main they would only be available to main.

Jim

Reputation: 0
• Posts: 22
• Joined: 13-February 11

## Re: Declare a variable and then use it inside a struct.

Posted 02 March 2012 - 11:33 AM

```#include <stdio.h>

void print_shape(struct d toPrint)
{
printf("%d, %d, %d, %d, %s\n", toPrint.shape.height, toPrint.shape.width, toPrint.depth, toPrint.numObjects, toPrint.x[0]);
}

struct a {
int height;
int width;
};

struct d {
struct a shape;
char x[32];
int depth;
int numObjects;
};

int main()
{

struct d solid[5];

solid[0].shape.height = 9;
solid[0].shape.width = 9;
solid[0].depth = 9;
solid[0].x[0] = 't';
solid[0].numObjects = 1;

print_shape(solid[0]);

return 0;
}

```

This is what i wrote but when i compile it i get a gazillion errors. Most likely because I'm butchering the C language.

These errors occur multiple times:

Warning: 'struct d' declared inside parameter list its scope is only this definition or declaration which is probably not what you want

Error: Parameter 1 'toPrint' has incomplete type

any idea why these occur? And thanks for the help guys i'll pass around the kudo's =)

well i guess Kudo's is something different on this site. I meant i'll hit the little green + button things =)

### #11 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: Declare a variable and then use it inside a struct.

Posted 02 March 2012 - 11:37 AM

First your structures must be before the function that is trying to use them.

Next post the complete error messages, exactly as they appear in your development environment.

Jim

Reputation: 0
• Posts: 22
• Joined: 13-February 11

## Re: Declare a variable and then use it inside a struct.

Posted 02 March 2012 - 11:50 AM

jimblumberg, on 02 March 2012 - 11:37 AM, said:

First your structures must be before the function that is trying to use them.

Next post the complete error messages, exactly as they appear in your development environment.

Jim

those errors both got cleared up by moving the structs to before the function thanks =)

i was wondering, for my
```solid[0].x[0] = 't';

```

if I wanted to fill out the array more than 1 char at a time if I wrote
```strcopy(solid[0].x, "Triangular Prism");

```

is that a valid way to do it? I tried but i get

Undefined first referenced
symbol in file
strcopy /var/tmp//ccQXD23R.o
ld: fatal: Symbol referencing errors. No output written to test.o
collect2: ld returned 1 exit status

whish means I need an #include i think? Sorry i'm new at this =(

### #13 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: Declare a variable and then use it inside a struct.

Posted 02 March 2012 - 11:56 AM

First the function name is strcpy(). Second did you include the string.h header file? Third, when you get errors post the relevant code along with the complete error message exactly as it appears in your development environment. These messages have important information embedded within them to aid in problem location and solving.

Jim

Reputation: 0
• Posts: 22
• Joined: 13-February 11

## Re: Declare a variable and then use it inside a struct.

Posted 02 March 2012 - 12:10 PM

Tyvm!

Alright the program works, and I think i understand most of it. Thank you for all your help!

Time to go get a new book I think =)