I understand how to create a struct on the heap using malloc. Was looking for some documentation regarding creating a struct in C on the stack but all docs. seem to talk about struct creation on heap only.
The same way you declare any variable on the stack:
struct my_struct {...};
int main(int argc, char **argv)
{
struct my_struct my_variable; // Declare struct on stack
.
.
.
}
To declare a struct on the stack simply declare it as a normal / non-pointer value
typedef struct {
int field1;
int field2;
} C;
void foo() {
C local;
local.field1 = 42;
}
an answer to 17.4 Extra Credit (in Zed's book "Learn C the Hard Way") using functions
#include <stdio.h>
struct Person {
char *name;
int age;
int height;
int weight;
};
struct Person Person_create(char *name, int age, int height, int weight)
{
struct Person who;
who.name = name;
who.age = age;
who.height = height;
who.weight = weight;
return who;
}
void Person_print(struct Person who)
{
printf("Name: %s\n", who.name);
printf("\tAge: %d\n", who.age);
printf("\tHeight: %d\n", who.height);
printf("\tWeight: %d\n", who.weight);
}
int main(int argc, char *argv[])
{
// make two people structures
struct Person joe = Person_create("Joe Alex", 32, 64, 140);
struct Person frank = Person_create("Frank Blank", 20, 72, 180);
//print them out and where they are in memory
printf("Joe is at memory location %p:\n", &joe);
Person_print(joe);
printf("Frank is at memory location %p:\n", &frank);
Person_print(frank);
// make everyone age 20 and print them again
joe.age += 20;
joe.height -= 2;
joe.weight += 40;
Person_print(joe);
frank.age += 20;
frank.weight += 20;
Person_print(frank);
return 0;
}
I got it to work this way:
#include <stdio.h>
struct Person {
char *name;
int age;
int height;
int weight;
};
int main(int argc, char **argv)
{
struct Person frank;
frank.name = "Frank";
frank.age = 41;
frank.height = 51;
frank.weight = 125;
printf("Hi my name is %s.\n", frank.name);
printf("I am %d yeads old.\n", frank.age);
printf("I am %d inches tall.\n", frank.height);
printf("And I weigh %d lbs.\n", frank.weight);
printf("\n-----\n");
struct Person joe;
joe.name = "Joe";
joe.age = 50;
joe.height = 93;
joe.weight = 200;
printf("Hi my name is %s.\n", joe.name);
printf("I am %d years old.\n", joe.age);
printf("I am %d inches tall.\n", joe.height);
printf("And I weigh %d lbs.\n", joe.weight);
return 0;
}
Related
so I have 3 structs here:
typedef struct {
char *name; // allocated
int commonality;
int weight;
} monster;
typedef struct {
char *name; // allocated
char *description; // allocated
double area;
int monsters;
monster **monsters; // allocated
} region;
typedef struct {
char *name; // allocated
double diameter;
int regions;
region **regions; // allocated
} planet;
I already have function:
monster *new_monster(char *name, int commonality, int weight);
I am trying to create these functions:
void add_monster_to_region(region *r, char *mname, int mcommonality, int weight);
void delete_region_from_planet(planet *p, char *rname);
This is what I have so far, but I don't believe it's right, can anyone tell me what I'm doing wrong and how I can fix it?
void add_monster_to_region(region *r, char *mname, int mcommonality, int mweight)
{
for (int i = 0; i < mcommonality; i++)
{
if (strcmp(mname, r->monsters[i]->name) == 0)
{
r->monsters[i]->name = mname;
}
}
}
Thank you
It would be very helpful to have a minimal reproducible example. It would also clarify what was meant with fully allocated NOT a reference array.
Nevertheless, here is my take on how to create a planet, two regions and how to populate the regions with monsters:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
char *name; // allocated
int commonality;
int weight;
} monster;
typedef struct {
char *name; // allocated
char *description; // allocated
double area;
int monster_count;
monster **monsters; // fully allocated, NOT a reference array
} region;
typedef struct {
char *name; // allocated
double diameter;
int region_count;
region **regions; // fully allocated, NOT a reference array
} planet;
monster *new_monster(char *name, int commonality, int weight) {
monster *new_monster = calloc(sizeof(monster), 0);
if (new_monster != NULL) {
new_monster->name = strdup(name);
if (new_monster->name == NULL) {
free(new_monster);
return NULL;
}
new_monster->commonality = commonality;
new_monster->weight = weight;
}
return new_monster;
}
void free_monster(monster *the_monster) {
if (the_monster != NULL) {
if (the_monster->name != NULL) {
free(the_monster->name);
}
free(the_monster);
}
}
void add_monster_to_region(region *r, char *mname, int mcommonality, int mweight)
{
monster *a_monster = new_monster(mname, mcommonality, mweight);
if (a_monster == NULL) return; // no memory
monster **monsters = NULL;
if (r->monsters != NULL) {
monsters = realloc(r->monsters, (r->monster_count + 1) * sizeof(monster *));
} else {
monsters = calloc(sizeof(monster *), 0);
}
if (monsters == NULL) {
free_monster(a_monster);
return; // no memory
}
r->monsters = monsters;
r->monsters[r->monster_count] = a_monster;
r->monster_count++;
}
void delete_region_from_planet(planet *p, char *rname) {
// TODO
}
int main() {
region desert = {"Desert", "Sunny and warm place.", 50.0, 0, NULL};
region ocean = {"Ocean", "Huge wet place.", 500.0, 0, NULL};
region *regions[] = {&desert, &ocean};
planet mud = {"Mud", 100.00, 2, regions};
add_monster_to_region(&desert, "Bug", 100, 100);
add_monster_to_region(&desert, "Zombie", 10, 20);
add_monster_to_region(&ocean, "Shark", 20, 40);
for (int i = 0; i < mud.region_count; i++) {
for (int j = 0; j < mud.regions[i]->monster_count; j++) {
printf("%s %d %d\n",
mud.regions[i]->monsters[j]->name,
mud.regions[i]->monsters[j]->commonality,
mud.regions[i]->monsters[j]->weight
);
}
}
// TODO: release allocated memory
return 0;
}
We can see all monsters in all regions of the defined planet:
$ gcc -Wall monsters.c
$ ./a.out
Bug 100 100
Zombie 10 20
Shark 20 40
$
#include <stdio.h>
struct Car {
char brand[50];
char model[50];
int year;
};
int main() {
struct Car car1 = {"BMW", "X5", 1999};
struct Car car2 = {"Ford", "Mustang", 1969};
struct Car car3 = {"Toyota", "Corolla", 2011};
printf("%s %s %d\n", car1.brand, car1.model, car1.year);
printf("%s %s %d\n", car2.brand, car2.model, car2.year);
printf("%s %s %d\n", car3.brand, car3.model, car3.year);
return 0;
}
/*
BMW X5 1999
Ford Mustang 1969
Toyota Corolla 2011
*/
Here the struct only has 3 variables (car1, car2, car3). But if it had numerous cars, how could I make this same code (print all values) using a loop?
You need an array of Cars, something like this
#include <stdio.h>
struct Car {
char brand[50];
char model[50];
int year;
};
int main() {
struct Car cars[] = {
{"BMW", "X5", 1999},
{"Ford", "Mustang", 1969},
{"Toyota", "Corolla", 2011},
{"Mercedes", "C197", 2010 }
};
for(size_t i = 0; i < sizeof(cars)/sizeof(cars[0]); ++i) {
printf("%s %s %d\n", cars[i].brand, cars[i].model, cars[i].year);
}
return 0;
}
You need an array of struct Car.
It works pretty much the same as any other array in with the difference that each element now has extra fields, the ones on your struct.
Here is an example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
struct Car {
char brand[50];
char model[50];
int year;
};
int main(void) {
struct Car cars[SIZE];
for (int i = 0; i < SIZE; i++) {
// Initialize
strcpy(cars[i].brand, "BMW"); // or any other brand
strcpy(cars[i].model, "X5");
cars[i].year = 1999;
}
for (int i = 0; i < SIZE; i++) {
// Print
printf("Car %d:\n", i + 1);
printf("%s %s %d\n", cars[i].brand, cars[i].model, cars[i].year);
}
return 0;
}
Recently, I took a subject called Data structure. I've created a program to try out my knowledge but don't know why the program did not work. I can't figure it out so I post it here to ask for solution. I hope people can help me. I am newbie here. So please ignore my opinion if my opinion is found annoying.
#include <stdio.h>
int main()
{
struct Book
{
char title[50];
int year;
float price;
};
int i;
struct Book books[50];
books[0].title="Bullshit";
books[0].year=132;
books[0].price=146.9;
books[1]=(struct Book){"Money",1344,189.4
};
for(i=0;i<2;i++)
{
printf("Book Title is : %s\n",books[i].title);
printf("Book Year is %d\n",books[i].year);
printf("Book price is %3.2f\n",books[i].price);
printf("\n\n");
}
}
1 I would make declaration of struct rather outside main than inside
2 Try changing char title[50] to char *title
#include <stdio.h>
struct Book {
char *title;
int year;
float price;
};
int main() {
int i;
struct Book books[50];
books[0].title = "Bullshit";
books[0].year = 132;
books[0].price = 146.9;
books[1] = (struct Book) {"Money", 1344, 189.4
};
for (i = 0; i < 2; i++) {
printf("Book Title is : %s\n", books[i].title);
printf("Book Year is %d\n", books[i].year);
printf("Book price is %3.2f\n", books[i].price);
printf("\n\n");
}
}
Why it didn't worked before?
In c arrays are not assignable by = operator.
You could do something like instead title[0] = 'B'; title[1] = 'u', etc....(or use strcpy which does it for you).
char *x is not really an array, it's just pointer to single char.
If we write x = "abc", we are telling the compiler: set x to 'a', next byte to 'b', next to 'c', and next to 0(not '0', just zero).
And when you do printf("%s",x), the printf function prints chars from the place in memory specified by x until it see 0 byte.
char *x = "abcd";
char *y = x;
while(*y != 0){ // this loop acts like printf("%s",x);
printf("%c",*y);
y++;
}
See also this and this question.
Or if you are using c++, not c, use std::string:
#include <cstdio>
#include <string>
struct Book {
std::string title;
int year;
float price;
Book(std::string t, int y, float p) {
title = t;
year = y;
price = p;
}
};
int main() {
int i;
Book books[50];
books[0].title = "Bullshit";
books[0].year = 132;
books[0].price = 146.9;
books[1] = Book(std::string("Money"), 1344, 189.4);
for (i = 0; i < 2; i++) {
printf("Book Title is : %s\n", books[i].title.c_str());
printf("Book Year is %d\n", books[i].year);
printf("Book price is %3.2f\n", books[i].price);
printf("\n\n");
}
}
this
books[0].title="Bullshit";
is not valid. title is defined as char[50]. Either do
strcpy(books[0].title, "BS");
This will copy the bytes of BS to title. Or do
struct Book
{
char *title;
int year;
float price;
};
...
books[0].title = strdup("BS");
This sets title up as a pointer to a char string. strdup will allocate space for the string and copy BS to that space. You will have to free that allocated space.
Or - the best. Use std::string
struct Book
{
std::string title;
int year;
float price;
};
....
books[0].title = "BS";
And as a final thought - life goes better with std::vector instead of raw arrays
There are quite a few topics on this subject but I haven't been able to find a solution that's worked for me; I am getting a segmentation fault whenever I try to change student_t.member->name. Below is the typedef structure that I am required to use:
typedef struct degree_t degree_t;
typedef struct student_t student_t;
struct degree_t {
student_t* member;
int course_id;
}
struct student_t {
char* name;
int age;
}
int main(int argc, char* argv[]) {
student_t *students = malloc(sizeof(student_t) * 3);
degree_t *degrees = malloc(sizeof(degree_t));
for (int i=0; i<3; i++) {
degrees[i].course_id = 1;
degrees[i].member->name = "Bob";
}
return 0;
}
I can change degrees[i].course_id perfectly fine, but whenever I try to change degrees[i].member -> name, I get a segmentation fault.
GDB indicates that this is a result of the line degrees[i].member->name = "Bob", but I'm don't understand why, and I don't know how to effectively change the value of degrees[i].member->name
Oops, you managed to place four errors in that small program.
The signature of main() should be main(int argc, char *argv[]). argv is an array of strings found on the command line.
You allocate memory for one degree, but in the for-loop you initialize three of them. This leads to a heap corruption.
You don't initialize degrees->member, but use it.
You allocate memory for three students, but don't use it.
Try this:
struct degree_t {
student_t* member;
int course_id;
}
struct student_t {
const char* name;
int age;
}
int main(int argc, char* argv[]) {
degree_t *degrees = malloc(3 * sizeof(degree_t));
for (int i=0; i<3; i++) {
degrees[i].course_id = 1;
degrees[i].member = malloc(sizeof(student_t));
degrees[i].member->name = "Bob";
degrees[i].member->age = 23;
}
return 0;
}
I am working through the Learn C the Hard Way and I am currently stuck on the extra credit exercise number 16.
I am currently trying to adapt their code and making the struct on the stack instead of the heap but my code is giving me segmentation fault and I am unsure why or how to proceed. Any advice is greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Person {
char *name;
int age;
int height;
int weight;
};
void Person_create(struct Person p,char *name,int age,int height,int weight)
{
p.name = name;
p.age = age;
p.height = height;
p.weight = weight;
}
void Person_print(struct Person who)
{
printf("Name: %s\n", who.name);
printf("\tAge: %d\n", who.age);
printf("\tHeight: %d\n", who.height);
printf("\tWeight: %d\n", who.weight);
}
int main(int argc, char *argv[])
{
// make two people structures
struct Person joe;
struct Person frank;
Person_create(
joe,"Joe Alex",32,64,140);
Person_create(
frank,"Frank Blank",20,72,180);
// print them out
Person_print(joe);
Person_print(frank);
// make everyone age 20 years and print them again
joe.age += 20;
joe.height -= 2;
joe.weight += 40;
Person_print(joe);
frank.age += 20;
frank.weight += 20;
Person_print(frank);
return 0;
}
The reason your code produces errors at runtime is that C passes structs by value, meaning that the following assignments inside Person_create have no effect:
p.name = name;
p.age = age;
p.height = height;
p.weight = weight;
This makes no changes to joe and frank inside main(), leading to undefined behavior on printing, because name data member of the struct Person remains uninitialized.
In order to fix this problem, pass struct Person by pointer, and apply -> operator in place of . operator:
void Person_create(struct Person *p,char *name,int age,int height,int weight)
{
p->name = name;
p->age = age;
p->height = height;
p->weight = weight;
}
...
Person_create(&joe, "Joe Alex", 32, 64, 140);
// ^ Pass an address
It is asking for me to not use pointers or malloc, and says as advice to look into creating a struct on the stack
Your code already creates the structs on the stack. If you would like to get rid of pointers completely, change Person_create to *return` a new person, like this:
struct Person Person_create(char *name,int age,int height,int weight) {
struct Person p;
p.name = name;
p.age = age;
p.height = height;
p.weight = weight;
return p;
}
...
joe = Person_create("Joe Alex", 32, 64, 140);
Define finction Person_create the following way
void Person_create(struct Person *p, char *name, int age, int height, int weight)
{
p->name = name;
p->age = age;
p->height = height;
p->weight = weight;
}
and call it like
Person_create( &joe, "Joe Alex", 32, 64, 140 );
A more sophisticated approach is the following
#include <string.h>
#include <stdlib.h>
//...
void Person_create(struct Person *p, const char *name, int age, int height, int weight)
{
size_t n = strlen( name );
p->name = malloc( ( n + 1 ) * sizeof( char ) );
strcpy( p->name, name );
p->age = age;
p->height = height;
p->weight = weight;
}
However in this case you should remember to free the allocated memory when the object of the structure leaves its scope.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *name;
int age;
int height;
int weight;
}Person;
void Person_print(Person who)
{
printf("Name: %s\n", who.name);
printf("\tAge: %d\n", who.age);
printf("\tHeight: %d\n", who.height);
printf("\tWeight: %d\n", who.weight);
}
int main(int argc, char *argv[])
{
Person joe= {"Joe Alex", 32, 64, 140};
Person frank={"Joe Alex", 32, 64, 140};
printf("Joe is at memory location: %p\n", &joe);
Person_print(joe);
printf("Frank is at memory location: %p\n", &frank);
Person_print(frank);
joe.age += 20;
joe.height -= 2;
joe.weight += 40;
Person_print(joe);
frank.age += 20;
frank.weight += 20;
Person_print(frank);
return 0;
}
Typedef the struct called 'Person', then you don't have to use 'Person create'.