void print_array not working as exprected
up vote
-1
down vote
favorite
I have this code written in C:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void print_array(char** array, int height, int width);
int main()
{
int height, width;
char **array = 0;
printf("Give height board size:");
scanf("%d", &height);
printf("Give width board size:");
scanf("%d", &width);
print_array(array, height, width);
return 0;
}
void print_array(char** array, int height, int width)
{
int i, j;
printf("n |"); for (j = 0; j < width; j++) printf("-");
printf("|n");
for (i = 0; i < height; i++)
{
printf(" |");
for (j = 0; j < width; j++) printf("%c", array[i][j]);
printf("|n");
}
printf(" |"); for (j = 0; j < width; j++) printf("-");
printf("|");
}
The expected result was this for 10x10
|----------|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|----------|
But the actual result for whatever number I give to Height is
E.g. Height 10 and Width 20
|--------------------|
|
If I run with Visual Studio I actually get and error code on line 32 which is the following
Exception thrown: read access violation. array was 0x1110112.
Line 32
for (j = 0; j < width; j++) printf("%c", array[i][j]);
c arrays
add a comment |
up vote
-1
down vote
favorite
I have this code written in C:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void print_array(char** array, int height, int width);
int main()
{
int height, width;
char **array = 0;
printf("Give height board size:");
scanf("%d", &height);
printf("Give width board size:");
scanf("%d", &width);
print_array(array, height, width);
return 0;
}
void print_array(char** array, int height, int width)
{
int i, j;
printf("n |"); for (j = 0; j < width; j++) printf("-");
printf("|n");
for (i = 0; i < height; i++)
{
printf(" |");
for (j = 0; j < width; j++) printf("%c", array[i][j]);
printf("|n");
}
printf(" |"); for (j = 0; j < width; j++) printf("-");
printf("|");
}
The expected result was this for 10x10
|----------|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|----------|
But the actual result for whatever number I give to Height is
E.g. Height 10 and Width 20
|--------------------|
|
If I run with Visual Studio I actually get and error code on line 32 which is the following
Exception thrown: read access violation. array was 0x1110112.
Line 32
for (j = 0; j < width; j++) printf("%c", array[i][j]);
c arrays
char **arrayis not an array, it's a pointer to pointer tochar.
– Fiddling Bits
Nov 19 at 20:58
3
Why do you even need an array? Can't you just useheightandwidth?
– Fiddling Bits
Nov 19 at 20:59
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have this code written in C:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void print_array(char** array, int height, int width);
int main()
{
int height, width;
char **array = 0;
printf("Give height board size:");
scanf("%d", &height);
printf("Give width board size:");
scanf("%d", &width);
print_array(array, height, width);
return 0;
}
void print_array(char** array, int height, int width)
{
int i, j;
printf("n |"); for (j = 0; j < width; j++) printf("-");
printf("|n");
for (i = 0; i < height; i++)
{
printf(" |");
for (j = 0; j < width; j++) printf("%c", array[i][j]);
printf("|n");
}
printf(" |"); for (j = 0; j < width; j++) printf("-");
printf("|");
}
The expected result was this for 10x10
|----------|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|----------|
But the actual result for whatever number I give to Height is
E.g. Height 10 and Width 20
|--------------------|
|
If I run with Visual Studio I actually get and error code on line 32 which is the following
Exception thrown: read access violation. array was 0x1110112.
Line 32
for (j = 0; j < width; j++) printf("%c", array[i][j]);
c arrays
I have this code written in C:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void print_array(char** array, int height, int width);
int main()
{
int height, width;
char **array = 0;
printf("Give height board size:");
scanf("%d", &height);
printf("Give width board size:");
scanf("%d", &width);
print_array(array, height, width);
return 0;
}
void print_array(char** array, int height, int width)
{
int i, j;
printf("n |"); for (j = 0; j < width; j++) printf("-");
printf("|n");
for (i = 0; i < height; i++)
{
printf(" |");
for (j = 0; j < width; j++) printf("%c", array[i][j]);
printf("|n");
}
printf(" |"); for (j = 0; j < width; j++) printf("-");
printf("|");
}
The expected result was this for 10x10
|----------|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|----------|
But the actual result for whatever number I give to Height is
E.g. Height 10 and Width 20
|--------------------|
|
If I run with Visual Studio I actually get and error code on line 32 which is the following
Exception thrown: read access violation. array was 0x1110112.
Line 32
for (j = 0; j < width; j++) printf("%c", array[i][j]);
c arrays
c arrays
asked Nov 19 at 20:57
user9874845
char **arrayis not an array, it's a pointer to pointer tochar.
– Fiddling Bits
Nov 19 at 20:58
3
Why do you even need an array? Can't you just useheightandwidth?
– Fiddling Bits
Nov 19 at 20:59
add a comment |
char **arrayis not an array, it's a pointer to pointer tochar.
– Fiddling Bits
Nov 19 at 20:58
3
Why do you even need an array? Can't you just useheightandwidth?
– Fiddling Bits
Nov 19 at 20:59
char **array is not an array, it's a pointer to pointer to char.– Fiddling Bits
Nov 19 at 20:58
char **array is not an array, it's a pointer to pointer to char.– Fiddling Bits
Nov 19 at 20:58
3
3
Why do you even need an array? Can't you just use
height and width?– Fiddling Bits
Nov 19 at 20:59
Why do you even need an array? Can't you just use
height and width?– Fiddling Bits
Nov 19 at 20:59
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
This answer is probably helpful to you. It gives an answer similar to @Govind Parmar's, but with this solution the array won't allocate a contiguous region of memory, which could give problems with functions that assume this.
Instead, you could do:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void print_array(char** array, int height, int width);
int main()
{
int height, width;
char** array;
char* temp;
printf("Give height board size:");
scanf_s("%d", &height);
printf("Give width board size:");
scanf_s("%d", &width);
array = malloc(height * sizeof(char*));
temp = malloc(height * width * sizeof(char*));
for (int i = 0; i < height; i++) {
array[i] = temp + (i * width);
}
print_array(array, height, width);
free(temp);
free(array);
return 0;
}
void print_array(char** array, int height, int width)
{
int i, j;
printf("n |"); for (j = 0; j < width; j++) printf("-");
printf("|n");
for (i = 0; i < height; i++)
{
printf(" |");
for (j = 0; j < width; j++) printf("%c", array[i][j]);
printf("|n");
}
printf(" |"); for (j = 0; j < width; j++) printf("-");
printf("|");
}
add a comment |
up vote
4
down vote
Since you are declaring array as a pointer to a pointer to char, but not defining it in read-only memory in the same line as declaration, you have to use the functions malloc to first allocate the array, and then later free it:
int i;
array = malloc(sizeof(char *) * height);
for(i = 0; i < height; i++)
array[i] = malloc(width);
// Use array....
for(i = 0; i < height; i++)
free(array[i]);
free(array);
That's if you actually need a 2-dimensional array. For your use case of printing out a square of arbitrary width and height, you don't.
Also note that the choice of allocating height as the first dimension, and then width as the second dimension, is completely arbitrary and it could just as easily be the other way around.
add a comment |
up vote
1
down vote
array is an uninitialized pointer-to-pointer. Attempting to dereference that array via the operator invokes undefined behavior.
You don't need an array here at all. Just print a space:
printf(" |");
for (j = 0; j < width; j++) printf(" ");
printf("|n");
2
I suspect the expectation was the (nonexistent) "array" was loaded with spaces already and the goal of the function was to exhibit that misinformed fact. Otherwise the function would have been more aptly named,print_box. =O
– WhozCraig
Nov 19 at 21:05
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
This answer is probably helpful to you. It gives an answer similar to @Govind Parmar's, but with this solution the array won't allocate a contiguous region of memory, which could give problems with functions that assume this.
Instead, you could do:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void print_array(char** array, int height, int width);
int main()
{
int height, width;
char** array;
char* temp;
printf("Give height board size:");
scanf_s("%d", &height);
printf("Give width board size:");
scanf_s("%d", &width);
array = malloc(height * sizeof(char*));
temp = malloc(height * width * sizeof(char*));
for (int i = 0; i < height; i++) {
array[i] = temp + (i * width);
}
print_array(array, height, width);
free(temp);
free(array);
return 0;
}
void print_array(char** array, int height, int width)
{
int i, j;
printf("n |"); for (j = 0; j < width; j++) printf("-");
printf("|n");
for (i = 0; i < height; i++)
{
printf(" |");
for (j = 0; j < width; j++) printf("%c", array[i][j]);
printf("|n");
}
printf(" |"); for (j = 0; j < width; j++) printf("-");
printf("|");
}
add a comment |
up vote
1
down vote
accepted
This answer is probably helpful to you. It gives an answer similar to @Govind Parmar's, but with this solution the array won't allocate a contiguous region of memory, which could give problems with functions that assume this.
Instead, you could do:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void print_array(char** array, int height, int width);
int main()
{
int height, width;
char** array;
char* temp;
printf("Give height board size:");
scanf_s("%d", &height);
printf("Give width board size:");
scanf_s("%d", &width);
array = malloc(height * sizeof(char*));
temp = malloc(height * width * sizeof(char*));
for (int i = 0; i < height; i++) {
array[i] = temp + (i * width);
}
print_array(array, height, width);
free(temp);
free(array);
return 0;
}
void print_array(char** array, int height, int width)
{
int i, j;
printf("n |"); for (j = 0; j < width; j++) printf("-");
printf("|n");
for (i = 0; i < height; i++)
{
printf(" |");
for (j = 0; j < width; j++) printf("%c", array[i][j]);
printf("|n");
}
printf(" |"); for (j = 0; j < width; j++) printf("-");
printf("|");
}
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
This answer is probably helpful to you. It gives an answer similar to @Govind Parmar's, but with this solution the array won't allocate a contiguous region of memory, which could give problems with functions that assume this.
Instead, you could do:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void print_array(char** array, int height, int width);
int main()
{
int height, width;
char** array;
char* temp;
printf("Give height board size:");
scanf_s("%d", &height);
printf("Give width board size:");
scanf_s("%d", &width);
array = malloc(height * sizeof(char*));
temp = malloc(height * width * sizeof(char*));
for (int i = 0; i < height; i++) {
array[i] = temp + (i * width);
}
print_array(array, height, width);
free(temp);
free(array);
return 0;
}
void print_array(char** array, int height, int width)
{
int i, j;
printf("n |"); for (j = 0; j < width; j++) printf("-");
printf("|n");
for (i = 0; i < height; i++)
{
printf(" |");
for (j = 0; j < width; j++) printf("%c", array[i][j]);
printf("|n");
}
printf(" |"); for (j = 0; j < width; j++) printf("-");
printf("|");
}
This answer is probably helpful to you. It gives an answer similar to @Govind Parmar's, but with this solution the array won't allocate a contiguous region of memory, which could give problems with functions that assume this.
Instead, you could do:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void print_array(char** array, int height, int width);
int main()
{
int height, width;
char** array;
char* temp;
printf("Give height board size:");
scanf_s("%d", &height);
printf("Give width board size:");
scanf_s("%d", &width);
array = malloc(height * sizeof(char*));
temp = malloc(height * width * sizeof(char*));
for (int i = 0; i < height; i++) {
array[i] = temp + (i * width);
}
print_array(array, height, width);
free(temp);
free(array);
return 0;
}
void print_array(char** array, int height, int width)
{
int i, j;
printf("n |"); for (j = 0; j < width; j++) printf("-");
printf("|n");
for (i = 0; i < height; i++)
{
printf(" |");
for (j = 0; j < width; j++) printf("%c", array[i][j]);
printf("|n");
}
printf(" |"); for (j = 0; j < width; j++) printf("-");
printf("|");
}
answered Nov 19 at 22:55
pooh17
444
444
add a comment |
add a comment |
up vote
4
down vote
Since you are declaring array as a pointer to a pointer to char, but not defining it in read-only memory in the same line as declaration, you have to use the functions malloc to first allocate the array, and then later free it:
int i;
array = malloc(sizeof(char *) * height);
for(i = 0; i < height; i++)
array[i] = malloc(width);
// Use array....
for(i = 0; i < height; i++)
free(array[i]);
free(array);
That's if you actually need a 2-dimensional array. For your use case of printing out a square of arbitrary width and height, you don't.
Also note that the choice of allocating height as the first dimension, and then width as the second dimension, is completely arbitrary and it could just as easily be the other way around.
add a comment |
up vote
4
down vote
Since you are declaring array as a pointer to a pointer to char, but not defining it in read-only memory in the same line as declaration, you have to use the functions malloc to first allocate the array, and then later free it:
int i;
array = malloc(sizeof(char *) * height);
for(i = 0; i < height; i++)
array[i] = malloc(width);
// Use array....
for(i = 0; i < height; i++)
free(array[i]);
free(array);
That's if you actually need a 2-dimensional array. For your use case of printing out a square of arbitrary width and height, you don't.
Also note that the choice of allocating height as the first dimension, and then width as the second dimension, is completely arbitrary and it could just as easily be the other way around.
add a comment |
up vote
4
down vote
up vote
4
down vote
Since you are declaring array as a pointer to a pointer to char, but not defining it in read-only memory in the same line as declaration, you have to use the functions malloc to first allocate the array, and then later free it:
int i;
array = malloc(sizeof(char *) * height);
for(i = 0; i < height; i++)
array[i] = malloc(width);
// Use array....
for(i = 0; i < height; i++)
free(array[i]);
free(array);
That's if you actually need a 2-dimensional array. For your use case of printing out a square of arbitrary width and height, you don't.
Also note that the choice of allocating height as the first dimension, and then width as the second dimension, is completely arbitrary and it could just as easily be the other way around.
Since you are declaring array as a pointer to a pointer to char, but not defining it in read-only memory in the same line as declaration, you have to use the functions malloc to first allocate the array, and then later free it:
int i;
array = malloc(sizeof(char *) * height);
for(i = 0; i < height; i++)
array[i] = malloc(width);
// Use array....
for(i = 0; i < height; i++)
free(array[i]);
free(array);
That's if you actually need a 2-dimensional array. For your use case of printing out a square of arbitrary width and height, you don't.
Also note that the choice of allocating height as the first dimension, and then width as the second dimension, is completely arbitrary and it could just as easily be the other way around.
edited Nov 19 at 21:25
answered Nov 19 at 21:04
Govind Parmar
6,68653053
6,68653053
add a comment |
add a comment |
up vote
1
down vote
array is an uninitialized pointer-to-pointer. Attempting to dereference that array via the operator invokes undefined behavior.
You don't need an array here at all. Just print a space:
printf(" |");
for (j = 0; j < width; j++) printf(" ");
printf("|n");
2
I suspect the expectation was the (nonexistent) "array" was loaded with spaces already and the goal of the function was to exhibit that misinformed fact. Otherwise the function would have been more aptly named,print_box. =O
– WhozCraig
Nov 19 at 21:05
add a comment |
up vote
1
down vote
array is an uninitialized pointer-to-pointer. Attempting to dereference that array via the operator invokes undefined behavior.
You don't need an array here at all. Just print a space:
printf(" |");
for (j = 0; j < width; j++) printf(" ");
printf("|n");
2
I suspect the expectation was the (nonexistent) "array" was loaded with spaces already and the goal of the function was to exhibit that misinformed fact. Otherwise the function would have been more aptly named,print_box. =O
– WhozCraig
Nov 19 at 21:05
add a comment |
up vote
1
down vote
up vote
1
down vote
array is an uninitialized pointer-to-pointer. Attempting to dereference that array via the operator invokes undefined behavior.
You don't need an array here at all. Just print a space:
printf(" |");
for (j = 0; j < width; j++) printf(" ");
printf("|n");
array is an uninitialized pointer-to-pointer. Attempting to dereference that array via the operator invokes undefined behavior.
You don't need an array here at all. Just print a space:
printf(" |");
for (j = 0; j < width; j++) printf(" ");
printf("|n");
answered Nov 19 at 21:01
dbush
90.8k12100131
90.8k12100131
2
I suspect the expectation was the (nonexistent) "array" was loaded with spaces already and the goal of the function was to exhibit that misinformed fact. Otherwise the function would have been more aptly named,print_box. =O
– WhozCraig
Nov 19 at 21:05
add a comment |
2
I suspect the expectation was the (nonexistent) "array" was loaded with spaces already and the goal of the function was to exhibit that misinformed fact. Otherwise the function would have been more aptly named,print_box. =O
– WhozCraig
Nov 19 at 21:05
2
2
I suspect the expectation was the (nonexistent) "array" was loaded with spaces already and the goal of the function was to exhibit that misinformed fact. Otherwise the function would have been more aptly named,
print_box. =O– WhozCraig
Nov 19 at 21:05
I suspect the expectation was the (nonexistent) "array" was loaded with spaces already and the goal of the function was to exhibit that misinformed fact. Otherwise the function would have been more aptly named,
print_box. =O– WhozCraig
Nov 19 at 21:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382523%2fvoid-print-array-not-working-as-exprected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
char **arrayis not an array, it's a pointer to pointer tochar.– Fiddling Bits
Nov 19 at 20:58
3
Why do you even need an array? Can't you just use
heightandwidth?– Fiddling Bits
Nov 19 at 20:59