Like Tree1Likes
  • 1 Post By stahta01

Undeclared Variable Error

This is a discussion on Undeclared Variable Error within the C Programming forums, part of the General Programming Boards category; Hello, I am writing a query for database here is the code.. Code: char *create_query(char *name, int perc) 43 { ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    21

    Question Undeclared Variable Error

    Hello,
    I am writing a query for database
    here is the code..
    Code:
    char *create_query(char *name, int perc)
     43 {
     44    char *defl = "INSERT INTO student_profile VALUES ('";
     45    char *def2 = "',";
     46    char *def3 = ");";
     47    char buf[33];
     48    char *query = (char *)malloc(300);
     49    strcat(query,def1);
     50    strcat(query, name);
     51    strcat(query, def2);
     52 
     53    itoa(perc, buf, 10);
     54 
     55    strcat(query, buf);
     56    strcat(query, def3);
     57 
     58    return query;
     59 }
    I am getting error

    03_Insert_into_Table.c: In function ‘create_query’:
    03_Insert_into_Table.c:49:2: warning: incompatible implicit declaration of built-in function ‘strcat’
    03_Insert_into_Table.c:49:15: error: ‘def1’ undeclared (first use in this function)
    03_Insert_into_Table.c:49:15: note: each undeclared identifier is reported only once for each function it appears in

    Even though i have declared def1 as char *, why im getting that error...
    also what is wrong with strcat....

    Thxn!!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,674
    For the first error it looks like a spelling error, look closely at the last character. For the second error it looks like you need to include a couple of include files for the functions you are using. Show what include files you are currently including. Also you should not be casting the return value of malloc().

    Jim

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    21
    here are the header file
    #include<my_global.h>
    #include<mysql.h>
    #include<stdlib.h>
    #include<string.h>

    I think the malloc cast is fine as it returns void * and i need to operate on char * in strcat..
    i solved strcat problem by including string.h...
    But def1 problem is still there....
    Last edited by greendragons; 12-21-2011 at 07:31 AM.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    defl (lowercase L) is not the same as def1 (numeral one)

    I think the malloc cast is fine as it returns void * and i need to operate on char * in strcat..
    No, you never need to cast the return value of malloc in C.

    and your first strcat should really be a strcpy.

    You can do all of what you're doing with a simple snprintf.

    Code:
    char *create_query(const char *name, int perc)
     {
         const char * const queryFormat = "INSERT INTO student_profile VALUES ('%s', %d)";
         char *query = malloc(300);
         if (query) {
             snprintf(query, 300, queryFormat, name, perc);
         }
         return query;
     }
    Last edited by rags_to_riches; 12-21-2011 at 07:39 AM.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    21
    gosh!! how can i miss that.. thnx alot rags....

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    21
    Quote Originally Posted by rags_to_riches View Post
    defl (lowercase L) is not the same as def1 (numeral one)



    No, you never need to cast the return value of malloc in C.

    and your first strcat should really be a strcpy.
    Yes your right it should be strcpy... can u please explain me why i dnt need to cast return poiner of malloc as i have been doing it in lot of programs... wht is wrong with it..
    I know we don't require that in C but in C++ cast is mandatory... but if i do cast i dnt think anything is wrong with it....
    Last edited by greendragons; 12-21-2011 at 07:41 AM.

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    If you're using C, and compiling as C, then there is no reason to cast it, and you should not do it. See the FAQ.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,478
    greendragons likes this.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    21
    Thnx alot!! i didn't know that....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undeclared variable problem
    By Furious5k in forum C++ Programming
    Replies: 2
    Last Post: 12-13-2008, 02:55 PM
  2. error: undeclared (need some help)
    By Nadril in forum C++ Programming
    Replies: 17
    Last Post: 02-22-2008, 11:59 AM
  3. Undeclared/un-initialized variable
    By jadedreality in forum C++ Programming
    Replies: 6
    Last Post: 10-26-2007, 11:16 AM
  4. undeclared indentifier error
    By SpEkTrE in forum C Programming
    Replies: 7
    Last Post: 11-18-2003, 06:08 PM
  5. Compiler error error C2065: '_beginthreadex; : undeclared identifier
    By Roaring_Tiger in forum Windows Programming
    Replies: 3
    Last Post: 04-29-2003, 01:54 AM

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