passing into functions

This is a discussion on passing into functions within the C Programming forums, part of the General Programming Boards category; EDIT: forget everything from before. It came out of confusion regard the supplier functions. dis_s() and read_s() the part functions ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    19

    passing into functions

    EDIT: forget everything from before. It came out of confusion regard the supplier functions. dis_s() and read_s() the part functions work and are not any different really.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    #define MPS 10   //Max Part Size
    #define MSS 10   //Max Supplier Size
    
    
    typedef struct
    {
       char name[30];
       char code[5];
       char street[30];
       char city[25];
       char state[3];
       int zip;
       int num;
    }SPLR; 
    
    
    
    
    typedef struct
    {
       char code[5];
       int qty;
       char s1_code[5];
       char s2_code[5];
       int num;
    }PART;
    
    
    void dis_s(SPLR* ptr1);
    
    
    void read_p(PART* ptr, char* file_name);
    
    
    void dis_p(PART* ptr);
    
    
    void read_s(SPLR* ptr1, char* file_name); 
     
    
    
    void process(SPLR* ptr1);
    
    
    int main()
    {
       PART e[MPS];
       SPLR e2[MSS];
       
       
       read_p(&e[0], "parts.data");
       read_s(&e2[0], "suppliers.data");
       
       process(&e[0], &e2[0]);
       
       return 0;
    }
    
    
    void read_p(PART* ptr, char* file_name)
    {
       
       int x=0;
       FILE *fpr;
       char buf[20];
       fpr=fopen(file_name,"r");
       while(!feof(fpr))
          {
          fscanf(fpr, "%s", &ptr[x].code[0]);
          fscanf(fpr, "%d", &ptr[x].qty);
          fscanf(fpr, "%s", &ptr[x].s1_code[0]);
          fscanf(fpr, "%s", buf);
          if (strcmp(buf, "--") != 0)   
          {
               strcpy(ptr[x].s2_code, buf);
          }
          else
          {
              ptr[x].s2_code[0] = '\0'; 
              x++;                          
              continue;                   
          }
                      
          fscanf(fpr, "%s", buf);
          
          
          x++;
          }
       
       ptr->num=x-1;
       fclose(fpr);
       return;
    
    
    
    
    }
    
    
    void read_s(SPLR* ptr1, char* file_name)
    {
       int x;
       FILE *fsr;
       char buf[50];
       fsr=fopen(file_name,"r");
       while(!feof(fsr)&&x<=MSS)
          {
             fscanf(fsr, "%s", &ptr1[x].name[0]);
             fscanf(fsr, "%s", &ptr1[x].code[0]);
             fscanf(fsr, "%s", &ptr1[x].street[0]);
             fscanf(fsr, "%s", &ptr1[x].city[0]);
             fscanf(fsr, "%s", &ptr1[x].state[0]);
             fscanf(fsr, "%d", &ptr1[x].zip);
             fscanf(fsr, "%s", buf);
             
             x++;
          }
       
     
        
       ptr1->num=x-1;   
       printf("%d", ptr1->num);
       fclose(fsr);
    
    
       return;
    }
    
    
    
    
    void dis_p(PART* ptr)
    {
       int i=0;
       
       while(i<=ptr->num-1)
       {
          printf("%s\n%d\n%s\n%s\n--\n", ptr[i].code, ptr[i].qty, ptr[i].s1_code,
                 ptr[i].s2_code);
          
          i++;
       }
    
    
    }
    
    
    void dis_s(SPLR* ptr1)
    {
       int i=0;
       
       while(i<=ptr1->num-1)
       {
          printf("%s\n%s\n%s\n%s\n%s\n%d\n--\n", ptr1[i].name, ptr1[i].code, 
                 ptr1[i].street, ptr1[i].city, ptr1[i].state, ptr1[i].zip);
          i++;
       }
    
    
    }
    
    
    void process(PART* ptr, SPLR* ptr1)
    {
       int x;
       char *pcode, *scode;
       FILE *fpr;
       pcode=(char*)malloc(20*sizeof(char));
       scode=(char*)malloc(20*sizeof(char));
       fpr=fopen("parts.data","r");
       FILE *fsr;
       fsr=fopen("suppliers.data","r");
       
       printf("  =====Here are you choices=====\n"
              "1. Provide supplier information for a part\n"
              "2. Provide part supplied by a supplier\n"
              "3. Add new supplier\n"
              "4. Add new part\n"
              "5. Add supplier for a part\n"
              "6. Display supplier list\n"
              "7. Display part list\n"
              "0. Quit\n" 
              "================================\n"
              "\n"
              "Enter your choice:");
              
       scanf("%d", &x);
              
       switch(x)
       {
         /* case 1:
             
             printf("Please enter the part code in question");
             scanf("%s", pcode);
             dis_sfp(ptr, ptr1, pcode);
             break;
          case 2:
             printf("Please enter the supplier code in question");
             scanf("%s", scode);
             dis_pbs(ptr, scode);     
             break;
          case 3:
             
             
             break;
          case 4:
            
             break;
          case 5:
             asp(ptr, scode, pcode);
             break;*/
          
          case 6:
             dis_s(ptr1);
             break;
          case 7:
             dis_p(ptr);
          case 0:
             printf("Goodbye");
             break;
          default:
             printf("Please make a selection from the designated list.\n");
             break;
         }    
             
    }
    when I run the dis_s() function it just prints out garbage until it segments. starts with a bunch of 0 and newlines until it starts printing locations on my computer...... it worked on campus with debian and I only copied it from gmail to my computer so I really don't understand whats up(im on mint). can anyone help.

    Here is the parts.data and supplier.data

    A001
    34
    c202
    a105
    --
    B002
    45
    c101
    a009
    --
    C008
    31
    e092


    --
    D006
    12
    a105
    g002
    --






    supplier


    ABC_Industries
    a009
    123_W_Woodlawn_Drive
    Wichita
    KS
    67229
    --
    Advanced_Systems
    a105
    34_E._London_Ave
    Wichita
    KS
    66234
    --
    Capital_Systems
    c101
    1820_Bridge_St
    Wichita
    KS
    67234
    --
    Consumer_Products
    c202
    37th_St
    Wichita
    KS
    66345
    --
    Evergreen_Industries
    e092
    56_Broadway_Ave
    Wichita
    KS
    66574
    --
    Goodwill_Industries
    g002
    23_E._Oxford_Circle
    Wichita
    KS
    67237
    --
    Last edited by Reboot.Revival; 02-18-2013 at 08:55 PM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    639
    ptr[x]->num I think is what you want but I could be misunderstanding.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    19
    Quote Originally Posted by camel-man View Post
    ptr[x]->num I think is what you want but I could be misunderstanding.
    If I were to include the [x] I would have to change it from a pointer and use ptr[x].num. but then it segment faults.

    read_p and dis_p work fine. I included them to show that even though being almost identical to read_s and dis_s, dis_s fails miserably.

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    639
    Well for one thing you are trying to pass too many arguments to process function. The prototype states that you only pass one thing to it yet in main you pass 2 things to it.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    It also helps if you compile with all warnings on:
    Code:
    $ make foo
    cc -Wall -Wextra -ggdb3    foo.c   -o foo
    foo.c: In function ‘main’:
    foo.c:59:4: warning: passing argument 1 of ‘process’ from incompatible pointer type [enabled by default]
    foo.c:47:6: note: expected ‘struct SPLR *’ but argument is of type ‘struct PART *’
    foo.c:59:4: error: too many arguments to function ‘process’
    foo.c:47:6: note: declared here
    foo.c: At top level:
    foo.c:168:6: error: conflicting types for ‘process’
    foo.c:47:6: note: previous declaration of ‘process’ was here
    foo.c: In function ‘process’:
    foo.c:176:10: warning: variable ‘fsr’ set but not used [-Wunused-but-set-variable]
    foo.c:172:10: warning: variable ‘fpr’ set but not used [-Wunused-but-set-variable]
    foo.c:171:18: warning: variable ‘scode’ set but not used [-Wunused-but-set-variable]
    foo.c:171:10: warning: variable ‘pcode’ set but not used [-Wunused-but-set-variable]
    make: *** [foo] Error 1
    Your function prototype and its definition don't match.

    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    Bye, Andreas

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Well firstly, I would fix the logical oddity in that you have a 'num' inside every 'PART', when clearly you only need it as a separate variable.
    Once you've corrected that, then start to fix the problem. There are a lot of off-by-one errors in the code.

    You're using feof wrong too. Check the FAQ.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    19
    The process function was just a bad copy and paste. I was trying to cut out parts of the code that I didnt need for the post and started cutting the "parts" area of the code. Just didnt get it put back in for the post everywhere when I decided to leave it. I put the num inside the structure because my professor had 2 other structures which carried the array of the structures and the count of the structures. I just thought it would be easier to get rid of them and put the count as num inside itself. My actual program compiles without warning. Again, the odd part on my end is that it runs fine on campus with debian(except the ptr->num), but not at home on mint. I am fairly certain that there is no difference. I just wanted to know why it isnt running on mint and why the prt->num isnt working.

    So these errors you are getting in the warning I understand them and they are not there in my full program. You get them because this is only a piece of it.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Reboot.Revival View Post
    The process function was just a bad copy and paste...My actual program compiles without warning...So these errors you are getting in the warning I understand them and they are not there in my full program.
    But posting some random code which has nothing to do with the original doesn't help and is just wasting our time.

    Quote Originally Posted by Reboot.Revival View Post
    Again, the odd part on my end is that it runs fine on campus with debian(except the ptr->num), but not at home on mint. I am fairly certain that there is no difference. I just wanted to know why it isnt running on mint and why the prt->num isnt working.
    As iMalc has already noted you have several buffer overflows. Thus the behaviour of your program is undefined. On one computer it seems to work and on another it crashes.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 01:17 PM
  2. Replies: 7
    Last Post: 04-19-2006, 12:17 PM
  3. Replies: 6
    Last Post: 05-06-2003, 04:08 PM
  4. passing functions
    By mcorn in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2002, 09:37 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

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