char pointers

This is a discussion on char pointers within the C Programming forums, part of the General Programming Boards category; Hello All, I've been working on a program which writes to MySQL. It needs some variables posting into the database ...

  1. #1
    jhp
    jhp is offline
    Registered User
    Join Date
    Jun 2011
    Posts
    6

    char pointers

    Hello All,

    I've been working on a program which writes to MySQL. It needs some variables posting into the database and I'm having trouble elegantly constructing a char * string to send to the MySQL connector.

    Here's what I'm doing at present, apologies for my awful code, it works but seems unnecessarily long-winded and ugly. Does anyone know of a more elegant way to insert variables into a char * string? If there were a need for lots of SQL queries this method could get ugly very quickly!

    Code:
    char *arg1a = "select * from database where field1 = '";
    char *arg1b = "' and field2 = '";
    char *arg1c = "'";
    
    char *sqlarg;
    
    sqlarg = (char *)malloc((strlen(variable1) + strlen(variable2) + strlen(arg1a) + strlen(arg1b) + strlen(arg1c) + 1)
    
    *sizeof(char));
    
    strcpy(arg1, arg1a);
    strcat(arg1, variable1);
    strcat(arg1, arg1b);
    strcat(arg1, variable2);
    strcat(arg1, arg1c);
    
    /* completed query now stored in sqlarg */
    Thanks for any pointers (heehee)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    See the make_message function at the end of snprintf(3): formatted output conversion - Linux man page

    Then you could do something like
    Code:
    sqlarg = make_message( "select * from database where "
                           "field1 = '%s' "
                           "and field2 = '%s'",
                           variable1, variable2 );
    if ( sqlarg != NULL ) {
      // do something SQL'ish
      free( sqlarg );
    } else {
      // panic
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    jhp
    jhp is offline
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    Thanks, Salem. That code threw up some scary errors which I'm not experienced enough to figure out, but you did put me on the right track, and this seems to have got me going:

    Code:
    sprintf(query, "insert into database values ('%s', '%s')", field1, field2);
    I read someplace this could be bad, but the snprintf manpage is, frankly, beyond my grasp at this point.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by jhp
    I read someplace this could be bad, but the snprintf manpage is, frankly, beyond my grasp at this point.
    Yeah the problem with the sprintf is the bufferoverflow. Hence recommanded to use snprintf, so the second parameter to snprintf would be the length or the number of bytes to copy into char *str from source, without bufferoverflowing.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and array of char[]
    By strokebow in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2008, 08:56 AM
  2. char pointers in C
    By Rishi Kumar in forum C Programming
    Replies: 22
    Last Post: 05-23-2008, 04:07 AM
  3. int & char pointers
    By cjohnman in forum C Programming
    Replies: 7
    Last Post: 05-02-2008, 02:46 PM
  4. Pointers to Characters... char *s
    By Krak in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2005, 02:47 PM
  5. use of pointers with char arrays
    By txp200 in forum C Programming
    Replies: 6
    Last Post: 05-12-2004, 07:52 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21