void print_array not working as exprected












-1














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]);









share|improve this question






















  • char **array is not an array, it's a pointer to pointer to char.
    – Fiddling Bits
    Nov 19 '18 at 20:58






  • 3




    Why do you even need an array? Can't you just use height and width?
    – Fiddling Bits
    Nov 19 '18 at 20:59
















-1














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]);









share|improve this question






















  • char **array is not an array, it's a pointer to pointer to char.
    – Fiddling Bits
    Nov 19 '18 at 20:58






  • 3




    Why do you even need an array? Can't you just use height and width?
    – Fiddling Bits
    Nov 19 '18 at 20:59














-1












-1








-1







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]);









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 20:57







user9874845



















  • char **array is not an array, it's a pointer to pointer to char.
    – Fiddling Bits
    Nov 19 '18 at 20:58






  • 3




    Why do you even need an array? Can't you just use height and width?
    – 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






  • 3




    Why do you even need an array? Can't you just use height and width?
    – 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












3 Answers
3






active

oldest

votes


















1














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("|");
}





share|improve this answer





























    4














    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.






    share|improve this answer































      1














      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");





      share|improve this answer

















      • 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













      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      1














      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("|");
      }





      share|improve this answer


























        1














        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("|");
        }





        share|improve this answer
























          1












          1








          1






          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("|");
          }





          share|improve this answer












          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("|");
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 22:55









          pooh17pooh17

          444




          444

























              4














              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.






              share|improve this answer




























                4














                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.






                share|improve this answer


























                  4












                  4








                  4






                  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.






                  share|improve this answer














                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 19 '18 at 21:25

























                  answered Nov 19 '18 at 21:04









                  Govind ParmarGovind Parmar

                  7,17553055




                  7,17553055























                      1














                      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");





                      share|improve this answer

















                      • 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


















                      1














                      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");





                      share|improve this answer

















                      • 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
















                      1












                      1








                      1






                      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");





                      share|improve this answer












                      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");






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      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
















                      • 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




















                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                      SQL update select statement

                      'app-layout' is not a known element: how to share Component with different Modules