Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #125940
| From | August Karlstrom <fusionfile@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Treating a multi-dimensional array as one-dimensional |
| Date | 2018-01-29 10:40 +0100 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <p4mq61$1k5n$1@gioia.aioe.org> (permalink) |
I need to define a function which can handle two-dimensional array
parameters of any size (both dimensions). The code below illustrates my
current approach.
#include <stdio.h>
#define LEN(arr) (sizeof (arr) / sizeof (arr)[0])
static void Print(const int *A, int m, int n)
{
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", A[i * n + j]);
}
putchar('\n');
}
}
int main(void)
{
int A[2][3] = {{1, 2, 3}, {4, 5, 6}};
Print((int *) A, LEN(A), LEN(A[0]));
return 0;
}
I posted this question on StackOverflow(*) and it was suggested that It
violates the strict aliasing rule. Is there a different way to solve
this problem?
*https://stackoverflow.com/questions/48491951/can-i-treat-a-multi-dimensional-array-as-a-one-dimensional-array
-- August
Back to comp.lang.c | Previous | Next — Next in thread | Find similar | Unroll thread
Treating a multi-dimensional array as one-dimensional August Karlstrom <fusionfile@gmail.com> - 2018-01-29 10:40 +0100
Re: Treating a multi-dimensional array as one-dimensional Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-01-29 11:00 +0000
Re: Treating a multi-dimensional array as one-dimensional supercat@casperkitty.com - 2018-01-29 07:58 -0800
Re: Treating a multi-dimensional array as one-dimensional Nick Bowler <nbowler@draconx.ca> - 2018-01-29 22:38 +0000
Re: Treating a multi-dimensional array as one-dimensional Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-01-29 23:07 +0000
Re: Treating a multi-dimensional array as one-dimensional Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-01-29 03:29 -0800
Re: Treating a multi-dimensional array as one-dimensional August Karlstrom <fusionfile@gmail.com> - 2018-01-29 21:35 +0100
csiph-web