Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Using Structures

variable type

  • Please log in to reply
7 replies to this topic

#1 Guest

Guest

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 914 posts
  • Programming Language:C

Posted 10 December 2009 - 10:16 PM

Structures are a very important and useful part of C programming. They are commonly called structs, because that is how they are named in C. Structs are used to group many variable types under one name.

You define a struct as follows:
struct name {
//variables go here
}; //don't forget this semicolon!

When you define a struct, you are defining your own variable type. You can declare variables with the the struct type as follows:
struct name variable;

It's just like you would declare an int, or any other type of variable.

The variables in a struct are called members, look at this struct definition:
struct intfloat {
int a;
float b;
};

This is a very simple struct with an integer, a, and a floating point number, b. Let's say you declare the struct like this:
struct intfloat test;

To access a member from the test variable, you must use the dot operator:
test.a=6;

This sets the member a as the number 6. You can do the same thing with member b:
test.b=1.5;

This sets b as 1.5 because b is a floating point number.

Typedef is a good way to save a lot of typing. It gives a data type a new name. You can do this with the struct definition:
typedef struct {
int a;
float b;
} intfloat;

When using a typedef, you don't need to use struct for every variable you declare, just do this:
intfloat test;

If you haven't used pointers before, please read this tutorial before moving on.
When you declare structs as pointers, you have to use the arrow operator to access members. Let's assume we already defined intfloat with a typedef, just like above:
intfloat test;
test.a=5;
test.b=4.3;
intfloat *pointer;
pointer=&test;
printf("%d, %f\n", pointer->a, pointer->b);
This code will print out:
5, 4.300000

The arrow operator is what dereferences the pointer members.

Here is an example in C that uses structs to show stock statistics:
#include <stdio.h>

typedef struct {
	char *name;
	float value;
	float change;
} stock;

stock setval(char *name, float value, float change) {
	stock temp;
	temp.name=name;
	temp.value=value;
	temp.change=change;
	return temp;
}

void printv(stock temp) {
	printf("%s stats:\nStock value is:%f, Stock change is:%f\n", temp.name, temp.value, temp.change);
}

int main() {
	stock google=setval("GOOG", 591.50, 2.48);
	printv(google);
	stock redhat=setval("RHT", 27.66, 0.53);
	printv(redhat);
	return 0;
}

When using structures, it is usually a good idea to have functions that make it easy to handle them. My setval function sets the statistics, my printv function prints out the stock statistics.

As always, feel free to post suggestions, questions, and positive feedback.
Remember, +rep is appreciated. ;)
  • 3
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 11 December 2009 - 08:05 AM

Nicely done. +rep :)
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Egz0N

Egz0N

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1155 posts

Posted 12 December 2009 - 10:40 AM

nice .. and +rep :)
  • 0

#4 Guest

Guest

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 914 posts
  • Programming Language:C

Posted 12 December 2009 - 11:04 AM

Thank you guys :D
@Egz0N: You filled up my inbox by responding to all the introductory threads :P
  • 0
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#5 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 12 December 2009 - 02:25 PM

repable. You said "Typedef is a good way to save a lot of typing." are there any negative side effects of using typedefs in terms of speed/performance/optimization/other?



Also to two important points I would like to append to your tutorial:

As an alternative to setting each member of the struct individually by using the dot operator, you can use an alternate syntax:
stock setval(char *name, float value, float change) {
    stock temp = {name, value, change};
    return temp;
}

You can also use arrays in structs. However, they must be of defined length unless they are the last member of the struct. For example:
typedef struct {
    //char str[] cannot go here
    int length;
    char str[];
} string;


Also, for those people who don't have a solid understanding of the difference between char *str and char str[] often run into the following problem.

typedef struct {
    int length;
    char str[];
} string;

int main() {
    //I can do this
    char bar[] = "Hello, World";

    //But I can't do this?
    //foo.str = "Hello, World";

    //Solution
    memcpy(foo.str, "Hello, World", 13);
    return 0;
}

Without getting into the details of pointers and rvalues and lvalues, it is hard to explain. So someone else can do that. Zeke may have in his tutorial?

Note: None of this code was sent through a compiler because I'm too lazy. In theory, it should work.

Edited by John, 12 December 2009 - 03:06 PM.

  • 0

#6 stranger

stranger

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 29 December 2009 - 02:41 PM

thnx bro
  • 0

#7 LuiDaHottest

LuiDaHottest

    CC Newcomer

  • Just Joined
  • PipPip
  • 18 posts

Posted 14 January 2010 - 08:06 PM

ohh yeah
  • 0

#8 Guest

Guest

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 914 posts
  • Programming Language:C

Posted 15 January 2010 - 01:01 AM

You said "Typedef is a good way to save a lot of typing." are there any negative side effects of using typedefs in terms of speed/performance/optimization/other?

As far as I know, there are no side effects to typedef. I guess the only problem would be if you used them when they weren't appropriate. For example, if you used typedef to make numbers ints, your source code would look weird declaring everything as numbers instead of ints.

(I was going to reply when you first posted, but I forgot until now :P)
  • 0
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)





Also tagged with one or more of these keywords: variable type

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download