Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
struct t{
char days[20];
int date;
char x;
struct t *next;
}*head
printf("%ld\n", sizeof(head));
where sizeof(*void)=8, sizeof(int)=4, sizeof(char)=1
Why does it print 8?
head is a pointer to the struct t, which is 8 bytes since I'm assuming you're running an x64 program. If you want the size of the underlying type, do this:
sizeof(*head)
Notice that head is a pointer to the struct rather than an actual instance of the struct. This means that sizeof(head) is the size of the pointer, which on your system happens to be 8 (notice that sizeof(void*) is also 8).
Hope this helps!
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Please clarify my doubts on structure pointer in below
I have structure like below
struct ip_node {
char arr[6];
};
typedef struct ip_node ip;
ip ip1={"abcde"};
case 1: // it is running without error
ip *ip2=&ip1;
case 2: // it is giving error
ip *ip2;
ip = &ip1;
I thought both cases are same.
Why does the second case give an error?
They do exactly same thing, assignin address of variable to pointer which is correct. You have just typo.
Change
ip = &ip1;
to
ip2 = &ip1;
Where you tried assign value to type.
Here is the correct example.
ip = &ip1;
is so invalid as the same as doing:
int = 1;
you need a name for that type ip, and that type should be a pointer because you are doing &ip1;
ip* a2 = &ip1;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a struct with dates and time.
struct datetime{
int hour;
etc...}
When I try to assign the correct hour to my own struct-hour, I get an error.
struct tm tm_struct = *localtime(time(NULL));
dt->hour = tm_struct->tm_hour;
First I get an incompatible pointer conversion error on the first line, then I get an error about using tm_struct.tm_hour instead of ->.
Any help would be appreciated :)
localtime expects a pointer to time_t as its argument. Instead you are passing time_t value itself, which is the root of your first problem. Typically because of this you will have to introduce an intermediate named time_t object to hold the result of time
time_t t = time(NULL);
struct tm tm_struct = *localtime(&t);
preventing you from using your original one-liner.
But by using compound literals you can still express it as a one-liner
struct tm tm_struct = *localtime(&(time_t) { time(NULL) });
if you so desire.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
double *p;
p = malloc(sizeof(p));
if (p != NULL)
{
*p = 5.15;
}
For some reason, p = malloc(sizeof(p));doesn't work. I try to allocate as much memory as p needs. What is wrong with that?
I try to allocate as much memory as p needs.
p itself (as a variable) has got the (own) memory allocated, what you're trying is basically allocate the memory for which p will be pointing to.
Here, p points to a double, so it needs to have a memory area to be able to store a double value. So, the allocation should be equal to the size of a double, i.e,
p = malloc(sizeof*p);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want to make a shallow copy on an entire struct which has the constant deceleration. But I want the struct that I am copying too to be non constant.
This is what I have done so far which is producing an error:
struct Student{
char *name;
int age;
Courses *list; //First course (node)
}Student;
void shallowCopy(const Student *one){
Student oneCopy = malloc(sizeof(one));
oneCopy = one; <--------------- ERROR POINTS TO THIS LINE
}
The compiler error I am getting:
Assignment discards 'const' qualifier from pointer target type.
I know I can remove the const from one or add the const to oneCopy, but I want to know if there is a way to make a shallow copy in this specific situation where Student one is a const and the copy Student oneCopy is not.
It should be:
Student* oneCopy = malloc(sizeof(*one));
*oneCopy = *one;
Because you want to assign the struct, not the pointer.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Got xCode 5.0.2 bought mac yesterday and do not understand why this simple code doesn't work.
#include "stdio.h"
int main(){
int N;
printf("vvedite koli4estvo dannih\n");//mistake and warning is here
scanf("%d", &N);
int *arr = new (int [N]);
return 0;
}
mistake is
expected expression
implicit declaration of function 'new' is invalid in c99
Your code is written in C but you are using new; a C++ operator. Use malloc instead.
int *arr = malloc(sizeof(int)*N); // allocates memory for N itegers