Unable to pass array as parameter, using void pointer, s.t. the contents too are accessed in the called function.
In the below program, there is one type of error, repeated twice, on execution in Cygwin.
Code:
#include <stdio.h> Code:
b.c: In function ‘foo’: Errors 2: Code:
b.c:3:28: warning: dereferencing ‘void *’ pointer |
What is the goal? What do you wish to accomplish with this code?
|
what do you want to print?
The pointer itself? printf("%p \n", p) The integer? printf("%i \n", *(int *)p) or ? |
As stated in my post's title ...
Quote:
passed in successive invocations. |
Quote:
If try to run with the simple modification, of having an integer specifier, for the output; still get the same error; apart from the usual issue of having another array (char), to be passed too, to the same function. |
Hope would get some help soon.
|
There are not much of things you can do with a piece of data without knowing its type. Printing hexdump might be an option.
Code:
#include <stdio.h> |
Quote:
Though have understood that cannot use one function, to access two different types of arrays, as the output specifier would differ then; and the contents' data type can not be stated beforehand. But, to get this information, am not clear how you intend to use hexdump(). On Cygwin, it prints: Code:
0100000002000000030000000400000005000000060000000700000008000000090000000a000000 Also, am not clear as to why fputc() prints a single character of newline to the stdout (default file descriptor), even if multiple characters are used instead of just: '\n'? |
Never mind, here is the fixed version of your original program:
Code:
#include <stdio.h> |
Quote:
Seems never read this aspect of the C language before. There were some inadverent typos, after the removal of which, the code is: Code:
#include <stdio.h> Though still can't understand why the output is like that, as shown below: Code:
The reason for choosing the expression of: *(char *)(p+i), or: *(int *)(p+i), was that the alternative forms of: *(char *)p[i], and *(int *)p[i], did not work; as gave error: Code:
$ cc f1.c -o f1 Error #2: By your original code, after removing just the typos: Code:
#include <stdio.h> Code:
$ cc f2.c -o f2 |
Quote:
Quote:
Also, there is no need to pass the address of the array variables to make them a pointer as they are already simply pointers to the base of the arrays. Try this: Code:
#include <stdio.h> Code:
printf("%d: %d\n", i, *(int *)(p+i)); This one: Code:
printf("%d: %d\n", i, *((int *)(p)+i)); You could do it with less parenthesis, but I tend to use them when needed to make things clear to myself! This also works for the same reason given... Code:
printf("%d: %d\n", i, *(int *)p+i); |
@OP Make sure to achieve warning-less compilation (use options -pedantic -W -Wall -Werror); you can access the array elements this way:
((int *)p)[i] or *((int *)p + i), ((char *)p)[i] or *((char *)p + i) |
Quote:
The usual idiom for a string constant is this. It will work for what you're doing. Code:
const char* c = "ABCDE"; Code:
char c[6]; |
Quote:
|
Quote:
Though compilation was done successfully, with the command: Code:
$ cc -pedantic -W -Wall -Werror f2.c -o f2 |
All times are GMT -5. The time now is 11:45 PM. |