You are here:

C/segment fault

Advertisement


Question
Hi Narendra ,Please help me in solving error comming in following code.

MYSQL_RES *result;
            result = mysql_store_result(&mysql);
            MYSQL_ROW row;
            int i;
            num_fields = mysql_num_fields(result);
            while ((row = mysql_fetch_row(result)))
           {
             unsigned long *lengths = mysql_fetch_lengths(result);
                  char insertquery[10000]="Insert into sunbeltsugar.listing_master_table set ";
                 
                
                 for(i = 0; i < num_fields; i++)
                    {
                      strcat(insertquery,seller_field[i]);
                      strcat(insertquery,"=");
                      strcat(insertquery,row[i]);//segment fault
                      strcat(insertquery,",");
        
                    }
                 printf("%s",insertquery);

           }
              mysql_close(&mysql);
               exit(0);



in above code if i printf value of row[i] then he is printing the value one by one.


But in strcat it is producing segment fault error.


Thanks in advance
Neetendra

Answer
You have posted partial code and I have to do lot of guess work!

Anyway, here is what I think is going wrong:
You have declated row as:
MYSQL_ROW row;
So, row is not a character string.
And you are trying to use it as a string at:
strcat(insertquery,row[i]);//segment fault

I think, this is the reason for crash.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.