views:

403

answers:

6

I have a pointer to pointer array. I am assigning each row in the while loop below and the printf inside the while loop shows each is assigned my id number 1-20.
After, out side of the while loop I iterate through the array and every element is written with id 20?
Any help is greatly appreciated. (FYI- I am using the Template2doc library and example1.c, at the bottom here- http://www.algonet.se/~thunberg/template2doc/c_version/docs/index.htm)

Below code only shows problem area, I took out the rest.

    char **tableData[500]={NULL};         
    char *myData[500][2]; 


while(rc == SQLITE_ROW){
 tableData[r] = myData[r];
 printf(*tableData[r]); <-- Displays id 1-20 fine
 r=r+1;
}//end while 

tableData[r+1] = NULL;//null terminated array

for (a=0; a<r; a++){
 printf("\n");
 printf(*tableData[a]); <--Displays 20 elements all of id 20?
}


outputFile=insertTableData(outputFile, dataMarker, fieldMarker, tableData);
+1  A: 

Well from what I can see right now, you're terminating your tableData at essentially r+2 from your last "duple" of information. Also, it might be a little clearer (maybe just for me) if you did tableData[0][1] or tableData[0][2] when printf'ing. That kind of makes it more clear that tableData is an index to a "structure" of 2, each containing a NULL terminated string. That might help with your debugging as well...

PiNoYBoY82
+1  A: 

As pointed out, you are assigning Null at r+2 position. And are you in any way modifying tableData or myData in between the while and for loop?

sachin
+1  A: 

Try this for debugging then :

printf("Character at 0x%x : %d", tableData[a], *tableData[a]);

May be the bug is in what you took out ?

shodanex
+2  A: 

How are you populating myData? I don't see a clear bug in the code example given, but I suspect the problem is that you are assigning a pointer to a buffer in myData without actually copying the contents, so that myData[0 .. r] all point to the same buffer, which will only store the most recent value read. Try this:

while(rc == SQLITE_ROW){
        tableData[r] = myData[r];
        if (r > 0)
            printf(*tableData[r-1]);
        r=r+1;
}//end while

That should print the ids from 1 to 19 fine. If it starts at id 2 instead of id 1, that suggests myData is not keeping a copy of the data, it's all pointing at the same location.

Tony
+2  A: 

You should create something that actually compiles and reproduces the problem. Not only will it help people help you, but in doing so you may very well find the problem yourself.

In your code excerpts we have no idea:

  • What rc is, how its value is set, or how its value is ever going to change and therefore terminate the loop
  • What the initial value of r is
  • What the actual contents of myData are

I created this code based on what you posted, which produces the same output from both loops. So either I've missed something in what you did post, or you left something important out.

int main( int argc, char** argv ) {

#define STRING  char *

STRING  dummy = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()";

STRING *tableData[500]={0};         
STRING myData[500][2];

int r=0;

while(r < 20){
        myData[r][0] = dummy+2*r;
        myData[r][1] = dummy+r;
        tableData[r] = myData[r];
        printf(*tableData[r]);
        printf("\n");
        r=r+1;
}//end while 

int a;

for (a=0; a<r; a++){
        printf(*tableData[a]);
        printf("\n");
}


}
Dave Costa
A: 

Yes, there were all pointing to the last value whe done. I went ahead and allocated memory space for each item, pointing to each accordingly.

Tommy