C Video Tutorial 5

C Struct Video TutorialToday I’m going to cover how to create and use a struct in C. Structs are used when you need more then one piece of data to describe one thing. You can’t use an array because an array only holds data with the same data type

I’ll specifically cover how to : Create a Struct, Get data from a Struct, Initialize a Struct, Pass a Struct to a Function, Use Typedef, Change a Value in a Struct, Use a Struct in a Function, ->, *(structName) and how a Struct is stored in memory. The code follows the video below.

If you like videos like this, it helps to tell Google+ with a click here

Code From the Video

CTutorial5_1.c

// structs are used when you need more then one 
// piece of data to describe one thing.
// We can't use an array because areas only hold one
// type of data.

#include <stdio.h>

// This creates a dog struct with the data needed

struct dog {

	// Labeled with const because they won't change
	
	const char *name;
	const char *breed;
	int avgHeightCm;
	int avgWeightLbs;

};

void getDogInfo(struct dog theDog){

	printf("\n");

	// Since a struct is initialized like an array you
	// may think you access the data like this theDog[0],
	// but they don't work that way. You need to use the
	// dot operator like this.
	
	printf("Name: %s\n\n", theDog.name);
	printf("Breed: %s\n\n", theDog.breed);
	printf("Avg Height: %d cm\n\n", theDog.avgHeightCm);
	printf("Avg Weight: %d lbs\n\n", theDog.avgWeightLbs);

}

void getMemoryLocations(struct dog theDog){

	printf("Name Location: %d\n\n", theDog.name);
	printf("Breed Location: %d\n\n", theDog.breed);
	printf("Height Location: %d\n\n", &theDog.avgHeightCm);
	printf("Weight Location: %d\n\n", &theDog.avgWeightLbs);

}

void main(){

	// Define a new dog by passing the values specific 
	// to this dog.
	// struct is the data type and cujo is the variable name

	struct dog cujo = {"Cujo", "Saint Bernard", 90, 264};
	
	// Now we can pass all the dog info easily
	
	getDogInfo(cujo);
	
	// Keynote Presentation --------------------
	// A struct defines a template for your data type
	// A struct variable defines the memory needed to fit the data
	
	// What happens if a struct is copied to another?
	// They point to the same memory locations
	
	struct dog cujo2 = cujo;
	
	getMemoryLocations(cujo);
	
	getMemoryLocations(cujo2);
	
}

CTutorial5_2.c

#include <stdio.h>

// This struct will be stored in the dog struct

	struct dogsFavs {
	
		char *food;
		char *friend;

	};

	// You can use typedef to shorten struct dog to
	// just dog by doing this instead
	
	typedef struct dog {

	const char *name;
	const char *breed;
	int avgHeightCm;
	int avgWeightLbs;
	
	struct dogsFavs favoriteThings;
	} dog;

void getDogFavs(dog theDog){

	printf("\n");

	// To get the data in the struct you just chain
	// dot operators.
	// Since struct dogsFavs favoriteThings; is in dog
	// you use that after theDog variable name like
	// any other variable
	
	printf("%s loves %s and his friend is named %s\n\n", theDog.name, 
		theDog.favoriteThings.food,
		theDog.favoriteThings.friend);

}

void setDogWeight(dog theDog, int newWeight){

	theDog.avgWeightLbs = newWeight;
	
	printf("The weight was changed to %d\n\n",
		theDog.avgWeightLbs);

}

// struct dog *theDog means the function expects a pointer
// of type struct dog

void setDogWeightPtr(dog *theDog, int newWeight){

	// You use *theDog to get the value at the pointers address
	// You have to surround a struct with parentheses 

	(*theDog).avgWeightLbs = newWeight;
	
	printf("The weight was changed to %d\n\n",
		(*theDog).avgWeightLbs);
		
	// You can use either (*theDog).avgWeightLbs or
	// theDog->avgWeightLbs
	
	// theDog->avgWeightLbs means the variable named 
	// avgWeightLbs in the struct theDog
	
	printf("The weight with -> %d\n\n",
		theDog->avgWeightLbs);

}

void main(){
	
	// We can also store structs in structs
	 
	dog benji = {"Benji", "Silky Terrier", 25, 9, {"Meat", "Joe Camp"}};
	
	getDogFavs(benji);
	
	// How do you change a value in a struct
	
	setDogWeight(benji, 11);
	
	printf("The weight in main() %d\n\n",
		benji.avgWeightLbs);

	// It prints the old weight because when a struct is
	// passed to a function a new struct is passed to the
	// function with Benjis values
	
	// You need to pass the structs pointer with &
	
	setDogWeightPtr(&benji, 11);
	
	printf("The weight in main() %d\n\n",
		benji.avgWeightLbs);

}

2 Responses to “C Video Tutorial 5”

  1. Anno says:

    However the address of the structures you print is the one in the stack, instead of the real address of the variable. Hence they are diffferent.

Leave a Reply

Your email address will not be published.

Google+