Like Tree2Likes
  • 1 Post By smokeyangel
  • 1 Post By arti

struct problem undeclared (first use in this function)

This is a discussion on struct problem undeclared (first use in this function) within the C Programming forums, part of the General Programming Boards category; I trying to do a program let user input a host name which can't more than 10 characters and follow ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    The program is hang and quit after finish input

    I trying to do a program let user input a host name which can't more than 10 characters and follow by input the IP address , the program need to validate the IP that user input is correct . The the end store the information in the struct and print out the host name and IP address once user finish input.(do 5 times)

    The program can be compile ,but the program is hang and quit after finish input,doesn't print out any result.

    Here is my code
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
     
    #define DELIM "."
    #define MAX 10
    
    
    typedef struct data
    {
           
       int a;
       int b;
       int c;
       int d;
       char host[MAX];
    }IP;
    //IP = {a,b,c,d,"hostname"};
    
    
    //struct data result[]={};
    
    
    
    
    /* return 1 if string contain only digits, else return 0 */
    int valid_digit(char *ip_str)
    {
        while (*ip_str) {
            if (*ip_str >= '0' && *ip_str <= '9')
                ++ip_str;
            else
                return 0;
        }
        return 1;
    }
    /* return 1 if IP string is valid, else return 0 */
    int is_valid_ip(char *ip_str)
    {
        int i, num, dots = 0;
        char *ptr;
        char *token;
        char *flds[5];
        IP ip;
        
        
        //struct data x;
     
        if (ip_str == NULL)
            return 0;
     
        ptr = strtok(ip_str, DELIM);
        
        //x.a= ptr;
        
        //printf("the %c",result[0].a);
        
         
        if (ptr == NULL)
            return 0;
     
        while (ptr) {
     
            /* after parsing string, it must contain only digits */
            if (!valid_digit(ptr)){
                printf("The ip address should contain digit only\n");
                return 0;
            }
            
            num = atoi(ptr);
            
            //printf("\npart 1 ip: %s\n",ptr);
            
     
            /* check for valid IP */
            if (num >= 0 && num <= 255) {
                /* parse remaining string */
                ptr = strtok(NULL, DELIM);
                 //printf("\npart 2 ip: %s",ptr);
                if (ptr != NULL)
                    ++dots;
                    
            } else{
                printf("The ip address range should be 0~25\n");
                return 0;
            }
                
            
            flds[i] = ptr;     
            i++;
            ptr = strtok(NULL, DELIM);
            
            ip.a = atoi(flds[0]);
            ip.b = atoi(flds[1]);
            ip.c = atoi(flds[2]);
            ip.d = atoi(flds[3]);
            strcpy(ip.host, flds[4]);
            
    
    
        }
     
        /* valid IP string must contain 3 dots */
        if (dots != 3){
            printf("invalid format for ip address. should be x.x.x.x\n");
            return 0;    
        }
        return 1;
        
    }
     
    int main()
    {
        int i;
        //IP ip;
        char *token;
        char *flds[5];
        char ip1[MAX];
        char hostname[MAX];
        //char ip1[] = "128.2500.0.1";
      
        //is_valid_ip(ip1)? printf("Valid\n"): printf("Not valid\n");
        //is_valid_ip(ip2)? printf("Valid\n"): printf("Not valid\n");
    
        for(i=0;i<6;i++){
            
            printf("Please enter host name\n");
            scanf("%s",&hostname);
            while( strlen(hostname) > MAX)
            {        
                printf("invalid please type again\n");
                scanf("%c",&hostname);
            }    
            
            printf("Please enter ip\n");
            scanf("%s",ip1);
            while(!is_valid_ip(ip1))
            {
                    printf("invalid , please enter again\n");
                    scanf("%s",ip1);
            }
        
            printf("Valid\n");
            printf("Host :%s\n",hostname);
            printf("The %d Ip address you entered:%s\n",i+1,ip1);
            
            //printf("Host :%s\n",hostname);
            //printf("IP = %d.%d.%d.%d\n", a,b,c,d);
        }
             
        system("PAUSE");
        return 0;
    }
    Last edited by Jing Shan; 05-18-2013 at 02:44 AM. Reason: problem fixed and turn into another error

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    As written, you have a global variable called IP, whose type is 'struct data'.

    Perhaps you forgot the typedef?
    Code:
    typedef struct data
    {
            
       int a;
       int b;
       int c;
       int d;
       char host[MAX];
    }IP;
    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
    Registered User
    Join Date
    May 2013
    Posts
    2
    Quote Originally Posted by Salem View Post
    As written, you have a global variable called IP, whose type is 'struct data'.

    Perhaps you forgot the typedef?
    Code:
    typedef struct data
    {
            
       int a;
       int b;
       int c;
       int d;
       char host[MAX];
    }IP;
    thx man , but the program still can't work , the program will hangs and exit without any result

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    How many characters do you need to store say
    192.168.100.200

    Is it for example more than #define MAX 10 ?

    What about say www.google.com, does that fit in 10 chars?

    > but the program still can't work , the program will hangs and exit without any result
    It's called debugging - good luck.

    Seriously, without you posting exactly what input you do provide, we're not going to be sitting around testing your program in the hope that we stumble into the same problem you're seeing.
    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.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Time to whip out a debugger and find out what's happening.

    I compiled your program and ran it with gdb, and got:

    Code:
    Please enter host name
    fefefe
    Please enter ip
    1.2.3.4
    
    Program received signal SIGBUS, Bus error.
    0x00000000004008d9 in is_valid_ip (ip_str=0x7fffffffdfd0 "1") at ../test.c:88
    88	        flds[i] = ptr;
    So, what's wrong there?
    Code:
    (gdb) print i
    $1 = 32767
    Uh oh! The value of i is way too big for the array. This is because you haven't initialised it to anything. Uninitialised local variables are full of garbage.

    This area looks a bit half finished. You don't seem to have a loop to set the other values in flds. You should initalise it to something too - otherwise if your user hasn't put in enough of an ip address the atoi() calls will go and try to read memory at some random address. More crashes, more hangs....
    NULL is probably a safe bet here, but check the docs to see what atoi() does with a NULL pointer.

    Valgrind is a great tool for detecting this sort of thing, if you're on Linux.
    AndiPersti likes this.

  6. #6
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by Jing Shan View Post
    I trying to do a program let user input a host name which can't more than 10 characters and follow by input the IP address , the program need to validate the IP that user input is correct . The the end store the information in the struct and print out the host name and IP address once user finish input.(do 5 times)

    The program can be compile ,but the program is hang and quit after finish input,doesn't print out any result.

    Here is my code
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
     
    #define DELIM "."
    #define MAX 10
    
    
    typedef struct data
    {
           
       int a;
       int b;
       int c;
       int d;
       char host[MAX];
    }IP;
    //IP = {a,b,c,d,"hostname"};
    
    
    //struct data result[]={};
    
    
    
    
    /* return 1 if string contain only digits, else return 0 */
    int valid_digit(char *ip_str)
    {
        while (*ip_str) {
            if (*ip_str >= '0' && *ip_str <= '9')
                ++ip_str;
            else
                return 0;
        }
        return 1;
    }
    /* return 1 if IP string is valid, else return 0 */
    int is_valid_ip(char *ip_str)
    {
        int i, num, dots = 0;
        char *ptr;
        char *token;
        char *flds[5];
        IP ip;
        
        
        //struct data x;
     
        if (ip_str == NULL)
            return 0;
     
        ptr = strtok(ip_str, DELIM);
        
        //x.a= ptr;
        
        //printf("the %c",result[0].a);
        
         
        if (ptr == NULL)
            return 0;
     
        while (ptr) {
     
            /* after parsing string, it must contain only digits */
            if (!valid_digit(ptr)){
                printf("The ip address should contain digit only\n");
                return 0;
            }
            
            num = atoi(ptr);
            
            //printf("\npart 1 ip: %s\n",ptr);
            
     
            /* check for valid IP */
            if (num >= 0 && num <= 255) {
                /* parse remaining string */
                ptr = strtok(NULL, DELIM);
                 //printf("\npart 2 ip: %s",ptr);
                if (ptr != NULL)
                    ++dots;
                    
            } else{
                printf("The ip address range should be 0~25\n");
                return 0;
            }
                
            
            flds[i] = ptr;     
            i++;
            ptr = strtok(NULL, DELIM);
            
            ip.a = atoi(flds[0]);
            ip.b = atoi(flds[1]);
            ip.c = atoi(flds[2]);
            ip.d = atoi(flds[3]);
            strcpy(ip.host, flds[4]);
            
    
    
        }
     
        /* valid IP string must contain 3 dots */
        if (dots != 3){
            printf("invalid format for ip address. should be x.x.x.x\n");
            return 0;    
        }
        return 1;
        
    }
     
    int main()
    {
        int i;
        //IP ip;
        char *token;
        char *flds[5];
        char ip1[MAX];
        char hostname[MAX];
        //char ip1[] = "128.2500.0.1";
      
        //is_valid_ip(ip1)? printf("Valid\n"): printf("Not valid\n");
        //is_valid_ip(ip2)? printf("Valid\n"): printf("Not valid\n");
    
        for(i=0;i<6;i++){
            
            printf("Please enter host name\n");
            scanf("%s",&hostname);
            while( strlen(hostname) > MAX)
            {        
                printf("invalid please type again\n");
                scanf("%c",&hostname);
            }    
            
            printf("Please enter ip\n");
            scanf("%s",ip1);
            while(!is_valid_ip(ip1))
            {
                    printf("invalid , please enter again\n");
                    scanf("%s",ip1);
            }
        
            printf("Valid\n");
            printf("Host :%s\n",hostname);
            printf("The %d Ip address you entered:%s\n",i+1,ip1);
            
            //printf("Host :%s\n",hostname);
            //printf("IP = %d.%d.%d.%d\n", a,b,c,d);
        }
             
        system("PAUSE");
        return 0;
    }
    It seem that you declared the variable of struct "IP" in the function is_valid_ip but you have to and it's best to declare like so
    Code:
    typedef struct data
    {
            
       int a;
       int b;
       int c;
       int d;
       char host[MAX];
    }data;
    
    //declare in main
    data ip;
    laserlight likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undeclared first use of function
    By TheXx11 in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2010, 12:15 AM
  2. Problem reading struct outside of function
    By synthetix in forum C Programming
    Replies: 15
    Last Post: 07-11-2009, 09:05 AM
  3. undeclared (first use in this function)
    By chocolatecake in forum C Programming
    Replies: 2
    Last Post: 03-31-2009, 09:07 AM
  4. struct instance "undeclared" (first use in this function)
    By SamuraiDave in forum C Programming
    Replies: 2
    Last Post: 09-14-2005, 03:21 AM
  5. struct function problem
    By totalfreeloader in forum C++ Programming
    Replies: 2
    Last Post: 01-09-2003, 10:26 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