void print_array not working as exprected
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 |
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 **array
is not an array, it's a pointer to pointer tochar
.
– Fiddling Bits
Nov 19 '18 at 20:58
3
Why do you even need an array? Can't you just useheight
andwidth
?
– Fiddling Bits
Nov 19 '18 at 20:59
add a comment |
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 '18 at 20:57
user9874845
char **array
is not an array, it's a pointer to pointer tochar
.
– Fiddling Bits
Nov 19 '18 at 20:58
3
Why do you even need an array? Can't you just useheight
andwidth
?
– Fiddling Bits
Nov 19 '18 at 20:59
add a comment |
char **array
is not an array, it's a pointer to pointer tochar
.
– Fiddling Bits
Nov 19 '18 at 20:58
3
Why do you even need an array? Can't you just useheight
andwidth
?
– Fiddling Bits
Nov 19 '18 at 20:59
char **array
is not an array, it's a pointer to pointer to char
.– Fiddling Bits
Nov 19 '18 at 20:58
char **array
is not an array, it's a pointer to pointer to char
.– Fiddling Bits
Nov 19 '18 at 20:58
3
3
Why do you even need an array? Can't you just use
height
and width
?– Fiddling Bits
Nov 19 '18 at 20:59
Why do you even need an array? Can't you just use
height
and width
?– Fiddling Bits
Nov 19 '18 at 20:59
add a comment |
3 Answers
3
active
oldest
votes
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 |
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 |
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 '18 at 21:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 22:55
pooh17pooh17
444
444
add a comment |
add a comment |
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 |
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 |
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 '18 at 21:25
answered Nov 19 '18 at 21:04
Govind ParmarGovind Parmar
7,17553055
7,17553055
add a comment |
add a comment |
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 '18 at 21:05
add a comment |
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 '18 at 21:05
add a comment |
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 '18 at 21:01
dbushdbush
93.7k12101134
93.7k12101134
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 '18 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 '18 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 '18 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 '18 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.
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 **array
is not an array, it's a pointer to pointer tochar
.– Fiddling Bits
Nov 19 '18 at 20:58
3
Why do you even need an array? Can't you just use
height
andwidth
?– Fiddling Bits
Nov 19 '18 at 20:59