C: Incompatible pointer to integer conversion [closed] - c

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.

Related

Structure pointer address assignment produces an error [closed]

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;

Is there a way to make a shallow copy of a constant struct to a non-constant struct? [closed]

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.

Getting error when trying to malloc memory to pointer variable declared separately [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
I have declared a char pointer in the following manner:
School *student[10];
for(i=0;i<10;i++){
*student[i] = malloc(sizeof(Student)); <--- Error points here
}
The error I get is:
incompatible types when assigning to type 'struct Student' from type 'void*'
Does anyone know why I am getting this error?
But how come if I were to allocate memory in the same line it would be with the star. For example: Student *name = malloc(sizeof(Student)); Why does this work? Im a bit confused
*student[i] = malloc(sizeof(School)); should be student[i] = malloc(sizeof(School));
students is an array of pointer to struct of type School. So you need to allocate for each pointer in that array. When you write *student[i] - you are dereferencing pointer i instead of allocating memory for it.
And as NicolasMiari pointed out, the sizeof operator must apply to School instead of student.
But how come if I were to allocate memory in the same line it would be with the star. For example: Student *name = malloc(sizeof(Student)); Why does this work? Im a bit confused
That's different. When you write Student *name = malloc(sizeof(Student)); you are both declaring a pointer and initialize it with malloc. You can do both steps in a single line like that. Alternatively, you declare it first, then assign it with malloc in a different line - in that case you must remove the asterisk.
You may want to refer to this question pointer initialization and pointer assignment.

Why is the sizeof() this struct 8? [closed]

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!

strcpy() with copying string from one structure to another [closed]

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
it seems that this is a duplicate question, but I searched stackoverflow's question about that point and non is like to my problem(I think)
I've two variables of a struct each has its own pointer to char, when I tried to copy from one variable's string to another variable's string, nothing happened, although no errors appear, just warning
implicit declaration of function strcpy
incompatible implicit declaration of built-in function 'strcpy'
I read from some questions on stackoverflow that you'd better to use strdup() function instead of strcpy() but when I did, I had an error
too many arguments to function 'strdup'
I read that there's a problem with strcpy() called "segmentation fault" and I knew it's about memory allocation, I don't totally understand what it's exactly and don't know if it's the problem with my code?
and this is my code
struct p
{
char *name;
struct p *next;
};
struct p *ptrary[10];
int main(void)
{
struct p p,q;
p.name="xyz";
p.next=NULL;
ptrary[0]=&p;
strdup(q.name,p.name);
ptrary[1]=&q;
printf("%s\n",ptrary[1]->name);
return 0;
}
so what is the problem and how I can solve it?
strdup() takes only one argument; it malloc's and returns a new block of heap memory containing the duplicated string. (See https://stackoverflow.com/questions/252782/strdup-what-does-it-do-in-c)
Which probably also points to the problem you were having before -- were you remembering to malloc the space for q to copy p's contents into?
change
strdup(q.name,p.name);
to
q.name = strdup(p.name);
see man page of strdup for further details. The strdup() function returns a pointer to a new string.
full code:
#include <stdio.h>
#include <string.h>
struct p
{
char *name;
struct p *next;
};
struct p *ptrary[10];
int main(void)
{
struct p p,q;
p.name="xyz";
p.next=NULL;
ptrary[0]=&p;
q.name = strdup(p.name);
ptrary[1]=&q;
printf("%s\n",ptrary[1]->name);
return 0;
}

Resources