Structure pointer address assignment produces an error [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 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;

Related

malloc() on double pointer [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 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);

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.

Initializer not a constant. Malloc [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am writing this in C:
char *IP = malloc(12 * sizeof(char));
But I get "Initializer is not a constant
Any ideas?
You are trying to assign a value to a variable outside of any function. In this case, you can only assign constant values, which are not the result of function calls or operations. For example, you can do
int i = 3;
but not
int i = pow(2, 2);
For what you want to do, you can declare the variable in the global scope, but then assign a value in the main.
try this
char *IP;
...
IP = malloc(12 * sizeof(char));//in main

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!

Resources