Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Here is the code:
#include <stdio.h>
int main ()
{
int c[4][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11},{12,13,14,15}};
int (*p)[3] = (int(*)[3])(c+2);
return 0;
}
Its interesting that it sets p to be:
{{8,9,10},{11,12,13},{14,15,-8224}}
I do not understand what is happening, especially in the 6th line of the code. Please help me!
int c[4][4] declares a 2D array with dimensions 4x4.
int (*p)[3] declares an array pointer to an array of 3 elements.
(int(*)[3])(c+2) invokes a pointer aliasing bug, by treating the address of c+2 as if there was an array of 3 elements there.
Pointer conversions between type int (*)[4] (the type of c+2) and type int(*)[3] are not safe. In practice, most compilers will likely give you some deterministic result from this code, but they are not required to do so. The program may as well crash and burn and then that's the programmer's fault.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I like to know is this code wrong. reasons pleasec
#include <stdio.h>
#include <string.h>
#include <malloc.h>
void func(char **c)
{
strcpy(c[0],"he");
strcpy(c[1],"h");
// memcpy(*c[2],"hell",sizeof("hell"));
// c[0]="hello";
//c[1]="hi";
}
int main()
{
char *arr[2];
arr[0]=malloc(sizeof(char[3]));
arr[1]=malloc(sizeof(char[2]));
func(arr);
printf("%s\n",arr[0]);
printf("%s\n", arr[1]);
return 0;
}
this line seems to allocate 3 * sizeof(char)
arr[0]=malloc(sizeof(char[3]));
this line seems to allocate 2 * sizeof(char)
arr[1]=malloc(sizeof(char[2]));
can array of pointers point to variable sized strings?
Yes.
is this code wrong.
func() is brittle in that it may exhibit undefined behavior depending on value of input.
No information about how many c[] may be indexed.
No information about how many bytes are available for strcpy(c[i], ....) to copy.
func() is OK in main() as it passes in a value that happens to not exceed the function limitations in OP's case.
func() is a wrong design in that is lacks parameters to guide its limitations and lacks null pointer error detection.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have tried looking around but was not able to find an answer for this. I found something explaining when we use a double asterisk, **, however, I am not sure whether this applied to my case.
I have come across embedded systems code which looks a bit foreign to me :
port0 = *(volatile int *)(0x1C002100)
what this operation is doing is that it reads the GPIO port whose address is 0x1C002100. However what is the deal with all those asterisks ?
I would have written this something like :
port0 = *0x1C002100
Are they doing some type of pointer type casting and hence we use 2 asterisks ? That would be my best guess. Thank you for your help !
Look at the expression, it first convert a integer constant to a pointer, then it deference that pointer, which will yield a integer. It is essentially the same as:
int *p = (volatile int *)(0x1C002100);
int n = *p;
port0 = n;
The first * denotes the pointer type, the second is a dereference operator.
However second line is invalid C code, since you cannot dereference a integer.
port0 = *0x1C002100;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
What are the differences between these three pointers in C? I’m confused on how they differ.
int* arr1[8];
int (*arr2)[8];
int *(arr3[8]);
int* arr1[8];
arr1 is an array of 8 pointers to int.
int (*arr2)[8];
arr2 is a pointer to array of 8 ints.
int *(arr3[8]);
Same as arr1. The brackets are superfluous.
You may want to read Right-Left rule on how to read complex C declarations.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
# include <stdio.h>
# include <stdlib.h>
int main(int argc, char *argv[])
{
int a[5][10][2];
int *p;
p = (int(*)[10][2])p;//Gives error!
return EXIT_SUCCESS;
}
I want to type cast p to type so that it can act as a pointer to the given 3-d array?Is there a way to do so.I am applyindg the idea that the type of a variable is everything except variable name.
Why are you trying to "typecast" anything? Why would you expect a value "typecasted" to (int(*)[10][2]) to be compatible with an int * pointer? And why does your original code assigns p to p, completely ignoring a?
This is what you can do
int a[5][10][2];
int (*p)[10][2] = a;
Now p is a pointer that can be used to access a, i.e. p[i][j][k] is equivalent to a[i][j][k]. No typecasting necessary.
If you write p = (int(*)[10][2])a; it won't give you any errors, may be a warning. You are thinking that p will be converted to pointer to a 3-D array, which is wrong. Try this statement after assigning a to p.
printf("addresses %u %u",p,p+1);
According to you, output should be something similar to this(lets say) "addresses 9990000 99940000", because you are thinking p is pointing to 3-D array. However you will get similar to "addresses 9990000 9990004", proving that p is a pointer to an integer.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Code 1:
int n;
int c[n];
scanf("%d",&n);
Code 2:
int n;
scanf("%d",&n);
int c[n];
The first one gives segmentation fault but second one works fine.
Since both code segments use an uninitialized variable, they invoke undefined behavior (Not to mention that you are passing in an int where you should be passing in an int*. With undefined behavior the compiler makes no guarantees about what will happen. It could crash as in the first case, produce no error in the second case, or make demons fly out your nose.
What's happening is that in once case n has a value that happens to be set to a writable address and in the other it doesn't.
int n; // n is uninitialized
int c[n]; // you're trying to declare an aray with an uninitialized value