Wht you mean this declaration..

C

code break

Hi all,

i come across the following declaration in some code.

int (*ptr)[ 10];

any feedback helps a lot for me.
 
M

mark_bluemel

code said:
Hi all,

i come across the following declaration in some code.

int (*ptr)[ 10];

any feedback helps a lot for me.

Find yourself a copy of "cdecl" :-

$ cdecl
Type `help' or `?' for help
cdecl> explain int (*x)[10]
declare x as pointer to array 10 of int
cdecl> explain int *x[10]
declare x as array 10 of pointer to int
 
P

p_cricket_guy

code said:
Hi all,

i come across the following declaration in some code.

int (*ptr)[ 10];

any feedback helps a lot for me.

Find yourself a copy of "cdecl" :-

<off topic>

This is off-topic but OP might be using cdecl and facing a
parse error, thus leading to further confusion.
popular versions of cdecl treat "ptr" as a keyword. (short for
"pointer").

cdecl> explain int (*x)[10]
declare x as pointer to array 10 of int
cdecl> explain int (*ptr)[10]
parse error
cdecl> explain int (*foo)[10]
declare foo as pointer to array 10 of int
cdecl>

-p_cricket_guy

<-- snipped -->
 
P

pete

code said:
Hi all,

i come across the following declaration in some code.

int (*ptr)[ 10];

any feedback helps a lot for me.

It's a pointer to an array of ten int.
It can be used to access a two dimensional array
through a single pointer.

/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

#define X 2
#define Y 10

int main(void)
{
int (*ptr)[Y];
int x, y;

ptr = malloc(X * sizeof *ptr);
if (ptr == NULL) {
puts("ptr == NULL");
exit(EXIT_FAILURE);
}
for (x = 0; x != X; ++x) {
for (y = 0; y != Y; ++y) {
ptr[x][y] = Y * x + y;
}
}
for (x = 0; x != X; ++x) {
for (y = 0; y != Y; ++y) {
printf("%2d ", ptr[x][y]);
}
putchar('\n');
}
free(ptr);
return 0;
}

/* END new.c */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top