Skip to content

Pointer (*) to [] #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion tests/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,68 @@ void struct_with_define()
is_eq(a.im, 12);
}

void test_pointer_malloc1() {
int m = 3, n = 5;
int *p;
p = malloc(m * n * sizeof(int));
int i, j, count = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
count++;
p[i+j*m] = count;
}
}
is_eq(p[0], 1);
is_eq(p[2+2*m], 13);
free(p);
}
void test_pointer_malloc2() {
int m = 3, n = 5;
int (*p)[n];
p = malloc(m * n * sizeof(int));
int i, j, count = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
count++;
p[i][j] = count;
}
}
is_eq(p[0][0], 1);
is_eq(p[2][2], 13);
free(p);
}

void test_pointer_malloc2const() {
int (*p)[5];
p = malloc(3 * 5 * sizeof(int));
int i, j, count = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 5; j++) {
count++;
p[i][j] = count;
}
}
is_eq(p[0][0], 1);
is_eq(p[2][2], 13);
free(p);
}

void test_pointer_malloc3() {
int m = 3;
int (*p)[5];
p = malloc(m * 5 * sizeof(int));
int i, j, count = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < 5; j++) {
count++;
p[i][j] = count;
}
}
is_eq(p[0][0], 1);
is_eq(p[2][2], 13);
free(p);
}

void test_atoi_post()
{
char * num[3] = {{"123"},{"987"},{"456"}};
Expand All @@ -287,7 +349,7 @@ void test_atoi_post()

int main()
{
plan(763);
plan(771);

struct_with_define();

Expand Down Expand Up @@ -577,5 +639,11 @@ int main()
diag("q_sort");
q_sort();

diag("pointer array (*)[]");
test_pointer_malloc1();
test_pointer_malloc2();
test_pointer_malloc2const();
test_pointer_malloc3();

done_testing();
}