The dot operator in a struct [duplicate] - c

This question already has answers here:
What is the difference between the dot (.) operator and -> in C++? [duplicate]
(14 answers)
Closed 8 years ago.
i am trying to understand what is the difference when using structs and typedefs to access some components
what is the difference between using the dot operator when dealing with structs
using the example below
so far i have tried this code
typedef struct _game{
int something;
char something_else;
} g;
if i use
g.something or g->something
what is the difference?
i have used both of them and they both return results but i still dont understand the difference
can somebody please explain this?

I'm assuming this is C. When asking language questions tag the language. There are many languages that look the same and can give you subtly different answers. C++ is a different language than C, btw.
In this statement,
typedef struct _game { int something; } g;
g is a type, not a variable. As such, g.something makes no sense. typedef means "type define". Instead, you would have
g my_g_instance;
g *my_g_ptr = &my_g_instance;
my_g_instance.something = 2;
my_g_ptr->something = 5;
The difference between . and -> is whether the variable to the left of the operator is a pointer or not.

Related

What is the significance of asterisks postfixing a variable type in a method header? [duplicate]

This question already has answers here:
difference between int* i and int *i
(9 answers)
Why is the asterisk before the variable name, rather than after the type?
(12 answers)
Closed 3 years ago.
I have been trying to learn C as a programming language, and have been trying to solve sample problems on site like LeetCode in C programs. When I was reading over some of the skeleton code that was provided as a function header for a problem on LeetCode that I want to solve in C, the function header had asterisks post fixing some of the types, specifically like this:
int* twoSum(int* nums, int numSize, int target, int* returnSize) {
/* Code goes here */
}
After doing a fair bit of reading, I learned that prefixing a variable with an asterisk when declaring a variable reserves the variable as a pointer, but I have not been able to find anything about what it means when the type specifier itself is post fixed with an asterisk.
The spaces there don't matter.
int* nums is identical to int *nums. So are int * nums and int*nums.
All four of these declare nums as pointer to int.
It's a matter of style preference (though I wouldn't use that last one), with no effect on the generated code.

Using typedef keyword in c [duplicate]

This question already has answers here:
How to create a typedef for function pointers
(3 answers)
Closed 5 years ago.
I was going through one of the lecture slide taught in the class which included a typedef keyword in it. As opposed to normal use of typedef keyword where we usually put a alias for particular data, there was no alias put up for that example. Following is the example:See the typedef in the slide
Is the use of typedef correct, or am I interpreting differently?
It is a function pointer. And it is perfectly valid syntax.
Ptr is declared as a pointer to a function where the function takes two int arguments and return an int. Here you can use Ptr as the alias to the typedefed type.

pointers to function in c? [duplicate]

This question already has answers here:
How do function pointers in C work?
(12 answers)
Closed 7 years ago.
help me to understand the copest of pointers to function with this following example .
i was referring balagurusamy book of 'c' but was unable to understand this concept
i also googled but didnt understood the concept
here is the code
#define PI 3.14
double y(double);
double cos(double);
double table (double(*f)(),double,double,double);
main()
{
printf("table of y(x)=2*x*x-x+1\n\n");
table(y,0.0,2.0,0.5);
printf("\ntable of cos(x)\n\n");
table(cos,0.0,PI,0.5);
}
double table(double(*f)(),double min,double max,double step)
{
double a, value;
for(a=min;a<=max;a+=step)
{
value=(*f)(a);
printf("%5.2f %10.4f\n",a,value);
}
}
double y(double x)
{
return(2*x*x-x+1);
}
if u could explain me in detail about the concept of "pointes to function" then it would be really very very helpful
The function is just a piece of machine code. Machine code is just bytes of data coding instructions for CPU. That is, on the lowest level, the code and the data are not fundamentally different. So the concept of pointer to code is not too different from the pointer to data.

Pointer C - declaration [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In C, what is the correct syntax for declaring pointers?
I am fighting with the c language. Pointers are new to me, and I think I am getting closer and closer to understanding them.
I have though one questions.
What is the difference between:
int k = 4;
int* pcp = &k;
and
int k = 4;
int *pcp = &k;
I cant seem to find any difference between these declarations of the pointer, is it just syntactical sugar - or is there any difference?
Thanks
There is no difference in those declarations, but there is a difference between the following two declarations:
int* p, p2; // declares a pointer to int and a regular int
and:
int *p, *p2; // declares two pointers to int
that might be hidden by your example.
So I prefer the second declaration.
you can try it out yourself. just type both, printf it and see what happens :P
if i'm not terribly mistaken though, it's the same ;)

What does the colon do in this struct definition? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does 'unsigned temp:3' means
I don't understand this struct definition. It seems illegal to me, but apparently it isn't:
typedef struct
{
unsigned i:1;
} my_struct;
I believe that marking the variable as unsigned without a type is the same as marking it as unsigned int. However, I'm completely baffled by the colon. What does it do? My guess is that it's some sort of initializer, but I haven't a clue what it means. Off the top of my head, the only place that I'd expect a colon in C is in the ternary operator, which this obviously isn't. So, this appears to be a construct with which I am not familiar. Can anyone explain what the purpose of the colon is in this struct definition? What exactly does the declaration unsigned i:1; mean?
It defines i to be of 1 bit width.
If i:x is given then it defines i to be x bits wide.

Resources