Say I'm using 2 structs in C like:
struct department {
char name[10];
int floor;
}
struct person {
char name[10];
struct department *dep;
};
What happens under the hood, when accessing a variable person_1 of type person like this:
(person_1.dep)->floor.
First, the dep pointer is dereferenced, but after, are the first 10 of the name of the department simply skipped over to get the 4 bytes of the floor, or is there more to it?
Thanks!
Related
I used pointer while printing structure elements but its working without using pointer in the print statement but shouldn't we use pointers to gather the element from the variable address why is it different in the structure case and its different in string also . Can anyone tell me why is that ?
#include <stdio.h>
int main(){
struct books{
char name[10];
int page;
};
struct books b1={"book1",1};
struct books *ptr;
ptr=&b1;
printf("%s %d",ptr->name,ptr->page);
}
printf("%s %d",*ptr->name,*ptr->page);
is wrong. A->B means (*A).B. You should do either
printf("%s %d",ptr->name,ptr->page);
or
printf("%s %d",(*ptr).name,(*ptr).page);
What MikeCAT said.
Beware though that an array would also be a pointer so *ptr->name would compile but will produce the first character of 'name'.
struct books{
char name[10];
int page;
};
ptr=&b1;
printf("%c",*ptr->name);
Pointers used when the object is too big and copying such object vastes time. But in your struct it takes 10 bytes for char array + 4 bytes for int and it's not a problem. In 64 bit machine pointers take 8 bytes in 32 bit 4 bytes. Suppose you have a big a struct and per object it takes 50 bytes of memory if you copy it will take more space and will be slower and by copying a brand new object will be created and that won't change anything in original one.
Let's see in practice when pointers can be used:
#include <stdio.h>
struct person{
char name[10];
int age;
};
void grow(struct person p) {
++p.age;
}
int main(){
struct person Mike = { "Mike", 20 };
grow(Mike);
printf("Name: %s\tAge: %d", Mike.name, Mike.age);
}
OUTPUT:
Name: Mike Age: 20
This code won't change age of Mike because void grow(struct person p) function copies struct person by creating a new object then increments age and at the end destroys struct person p. If you pass it as pointer then Mike will be modified.
So, let s say i have the following structure
typedef struct
{
void * value;
}Info;
typedef struct
{
char* title;
int nrOfPages;
}Book;
I allocate memory in main for Book snowWhite and then i want to call a function that will allocate an Info struct, and will make the value pointer, point to snowWhite.For this i was thinking to make:
((Info*)p)->value = snowWhite
(i declare snowWhite in main as a Book type pointer: Book* snowWhite)
Is it correct? Will this (((Info*)p)->value = snowWhite) make the value pointer to the memory that i have allocated to snowWhite?
This is part of a homework assignment that I've benn struggeling with for days now.
So we define this struct
typedef enum { male = 0, female } gender;
struct person {
char name[30];
gender genderValue;
int age;
float weight;};
Now, we are given this function
int add(char* name, char* genderValueString, int age, float weight){}
And we are asked to construct a new person inside that function.
I tried different methods but keep getting errors and can't even get the name right.
struct person newperson = {.name = *newname, //...rest of code..};
only read the first character and ignores the rest.
How do I get the values passed into a newperson person.
This doesn't work either :
char newname[30];
strcpy_s(newname, 30, name);
struct person newperson = { .name = newname //...}
There's two problems:
The first is that you dereference the pointer variable newname. That gives you the value the pointer is pointing to, which is really just the first letter of the string.
The second problem is that you can't initialize an array with a pointer or another array. You have to copy arrays. For string you should use strcpy (or strcpy_s if it's available).
My understanding of the following structure code is that the last "s1" creates an instance of our new data type;
struct student
{
int age;
char *name;
}s1;
The addition of s1 is as if I had typed the following (after creating the structure type)
struct student s1;
However, if I use typedef, the code does not create an instance; instead it just makes s1 a synonym for struct student. Is this interpretation correct? I ask only because I find it odd that it doesn't work the same as the first block of code
typedef struct student
{
int age;
char *name;
}s1;
This doesn't create an instance of struct student called s1; it makes s1 the equivalent of typing struct student. What I find odd about this is that I thought typedef itself was enough to avoid typing struct student all the time. That is, if I had excluded s1 from the code block above, I could simply type
student s1;
instead of
struct student s1;
thanks to the addition of typedef.
If that is the case, then isn't this redundant:
typedef struct student
{
int age;
char *name;
} student;
I am already able to type simply student have C "substitute" in struct student simply by the first line of that last code block??
That's the way typedef works. By prepending it to what would otherwise be a variable definition, you create a declaration of a type alias. It's easier to see with a fundamental type:
int a; // a is a variable of type int
typedef int b; // b is a synonym for the type int
"What I find odd about this is that I thought typedef itself was enough to avoid typing struct student all the time. That is, if I had excluded s1 from the code block above, I could simply type student s1;"
No, that wouldn't work. Try it.
A typedef without a trailing name is syntactically correct, but meaningless.
int; // compiles, does nothing
typedef int; // compiles, does nothing
I have trouble with structures in c.
I have two structures like
typedef struct
{
char isim[256];
int deger;
struct ekstra *sonra;
}ekstra;
typedef struct
{
char *name;
int val;
struct ekstra *next;
}node;
/*and main is*/
int main()
{
int i;
node dizi[12];
for(i=0;i<12;i++)
{
dizi[i].name = malloc("asdasd"*sizeof(int));
strcpy (dizi[i].name,"asdasd");
/*and trouble starts here*/
**dizi[i].next = malloc(sizeof(ekstra));
printf("%s",dizi[i].next->isim);**
}
}
the error is
error: dereferencing pointer to incomplete type
How can I hold place for dizi[i].next?
struct ekstra is not the same as ekstra.
Your first struct typedef should be declared as follows:
typedef struct ekstra
{
char isim[256];
int deger;
struct ekstra *sonra;
}ekstra;
typedef struct... ekstra;
This means: "create a type that is called ekstra". From now on this type can be used just as any variable type (int, char etc).
struct ekstra *next;
This means: Somewhere in my program there is a struct of some type, I don't know what it contains, but I want to point to an element of that type. This is the meaning of incomplete type.
To fix your problems, simply replace this row with ekstra *next;.
More comments not directly related to the question:
dizi[i].name = malloc("asdasd"*sizeof(int));
This is pure nonsense code. It means: "Create a constant string literal in the ROM part of my program. In this constant string literal, store the letters "asdasd" and a null termination character. Then take the address of this ROM memory location, which is completely irrelevant to my application, convert it to an integer so that I get a 32-bit nonsense number. Then multiply this nonsense number with the sizeof an int, which doesn't make any sense to anyone either. Then take this completely nonsense result and allocate a random amount of dynamic memory based on this. Then watch the program crash.
I don't understand code line like
malloc("asdasd"*sizeof(int));
But, I think you problem should slove like this
dizi[i].next = (ekstra *)malloc(sizeof(ekstra));
and you struct define should like
typedef struct node{
int a;
int b;
struct node *next;
}node;