Question

I am trying to print an array of structs and receiving an access violation. Not sure how to fix this. i, j, k are 0 in at the break. Any help is appreciated.

Update:Rephrasing the question, What is the correct way to access the data inside the struct? Also qwr's answer looks promising, I will have time to test later, thanks.

my struct printing array of structs

Was it helpful?

Solution

About access violation:

Access Violation is generally an attempt to access memory that the CPU cannot physically address.

Common causes:

Dereferencing NULL pointers . (THIS IS YOUR CASE see picture). You want to access to 0x000(NULL) location

Attempting to access memory the program does not have rights to (such as kernel structures in process context)

Attempting to access a nonexistent memory address (outside process's address space)

Attempting to write read-only memory (such as code segment)

More :more here in wikipedia

How you should allocate (using your structure):

First You should allocate memory;And dont forget you should write its free function,too

Simple allocation would be such:

   //instead I advice PCurrentData notation 
   CurrentData AllCurrentData=malloc(NumProduct* sizeof *CurrentData);
   //memset so we can then check it with null
   memset(AllCurrentData,0,NumProduct* sizeof *CurrentData);
   int i,y;
   for(i=0;i<NumProduct;i++){
      //Noncontiguous allocation.Suits to string allocation,cause string lens differs
      AllCurrentData[i].data=malloc(COLUMNS*sizeof(char**));
      memset(AllCurrentData[i].data,0,COLUMNS*sizeof(char**));
      for(j=0;j<COLUMNS;j++){
         //this is just example
         //probably you want to allocate null terminating string
          int size=strlen(your_null_terminating_string)+1;//+1 for adding null,too
          AllCurrentData[i].data[j]=malloc(size*sizeof(char));
          memcpy(AllCurrentData[i].data[j],your_null_terminating_string,size);

       }

    }

Additionaly, What else is wrong in your code and proper way

Plus you are doing wrong. Looking at your structure you hold char**.But you try to access it with 2D [][] array. Why we can not? Cause there is no guarantee that char** reference to continguous allocation, or only it done ,by manually, the way that compiler can access to it with [][]. Plus for char** mostly allocation done with noncontinguous way.cause strings len differs

So Note these:

char** is not equal to 2D array char v[][] array. So doing AllCurrentData[i].data[j][k] is wrong to address char**.

only char* can be equally treated as one dimensional char v[] array. char[] decay to char*

So accessing should be done this way:

#define NULL 0
if(AllCurrentData[i]!=NULL && AllCurrentData[i].data[j]!=NULL){
     char* str=AllCurrentData[i].data[j];
     if(str!=NULL)
        while (str[k]!='\0'){
          printf("%c ",str[k]);++k;
         }
     }
      /*or
       while (*str){
           printf("%c ",*str);++str;
       } */
     /*but actualy you can print null terminating string directly
      (assuming you wanted this)
        if(str!=NULL)
                 printf("%s",str); 
       */

more to learn about arrays and pointers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top