Thread: C how to make a strncpy with fgets and pointers

  1. #1
    Registered User Alexander87's Avatar
    Join Date
    Mar 2013
    Location
    Jacksonville, Florida, United States
    Posts
    11

    C how to make a strncpy with fgets and pointers

    Why oh why will this code not work? It cashes the program on me once i get to it...

    Code:
        memset(&input, 0, sizeof(input));
        printf("Enter your firstname: ");
        pinput = fgets(pinput, BUFF, stdin);
        strncpy((pct + *pctcounter-1) ->firstName,pinput , strlen(pinput) - 1);
    I will be forever grateful if someone shows me the right way to implement this =).

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try explaining, first, what input, pinput, BUF, pct, pctcounter, and firstName are. We're not mindreaders, you know.

    Then explain what you expect that code to achieve and, more importantly, why you expect it will even come close to achieving it.

    As to why it is crashing, that is easy. You are molesting pointers. The problem is, you haven't provided enough information so that any mere mortal (without ability to read your mind) can work out what pointers are being molested.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User Alexander87's Avatar
    Join Date
    Mar 2013
    Location
    Jacksonville, Florida, United States
    Posts
    11

    Sorry Grumpy.... I am new to programming in general but here I ll post the whole prog

    Quote Originally Posted by grumpy View Post
    Try explaining, first, what input, pinput, BUF, pct, pctcounter, and firstName are. We're not mindreaders, you know.

    Then explain what you expect that code to achieve and, more importantly, why you expect it will even come close to achieving it.

    As to why it is crashing, that is easy. You are molesting pointers. The problem is, you haven't provided enough information so that any mere mortal (without ability to read your mind) can work out what pointers are being molested.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #define BUFF 120
    #define SIZE 9
    #define STND 20
    #define CLIENTS 50
    
    struct client
    {
        char firstName[STND];
        char lastName[STND];
        char ID[STND];
        char email[STND];
        float funds;
     //   float wager;
    }typedef client;
    
    // FUNCTIONS <<<<<<<<<<<<<>>>>>>>>><<<<<<>>>>><<<>>><<>><<>>><<>><><
    void menu();
    char getct(client *,int *);
    char checkNameTypeA(char *);
    void testdisplay(struct client *pct);
    
    int main()
    {
    
        client ct[CLIENTS];
        client *pct = &ct[0];
    
        int clientCounter = 0;
        int *pctcounter = &clientCounter;
    
    
        menu();
    
    
    
    
    
        int x = 0;
        while(x != 4)
        {
            printf("enter selection : ");
            scanf("%d", &x);
        switch(x)
            {
            case 1:
               getct(pct, pctcounter);
                break;
            case 2:
    
                break;
            case 3:
    
                break;
            case 4:
                return EXIT_SUCCESS;
                break;
                }
        }
    
    
        return EXIT_SUCCESS;
    }
    //************************************************************************************
    void menu()
    {
        printf("***************************************************************************\n");
        printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
        printf("**************WELCOME************VIRTUAL***********************************\n");
        printf("************************TO********************CASINO***********************\n");
        printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
        printf("***************************************************************************\n\n\n");
        printf("---Register #1\n\n");
        printf("---ATM Machine #2\n\n");
        printf("---Play #3\n\n");
        printf("---QUIT #4\n\n");
    }
    //************************************************************************************
    
    //***************************************************************************
    /*void displaytest(client *pct)
    {
       printf("Student ID: %s\n", pct->firstName );
       printf("Student Phone: %s\n", pct->lastName);
       printf("Student GPA: %s\n",  pct->ID);
    }*/
    //****************************************************************************
    char getct(client * pct, int *pctcounter)
    {
        char * input[BUFF];
        char *pinput = NULL;
     //   float funds = 0;
     //   float wager= 0;
    
     //   fflush(stdin);
    
        fflush(stdin);
        printf("Enter your firstname: ");
        pinput = fgets(input, BUFF, stdin);
        strncpy((pct + *pctcounter-1) ->firstName,pinput , strlen(pinput) - 1);
        memset(&input, 0, sizeof(input));
    
    
    
        printf("Enter your lastname: ");
        pinput = fgets(input, BUFF, stdin);
        strncpy(pinput,(pct + *pctcounter-1) ->lastName , strlen(pinput) - 1);
        memset(&input,0, sizeof(input));
    
        printf("Enter your email: ");
        pinput = fgets(input, BUFF, stdin);
        strncpy((pct + *pctcounter-1) ->email ,pinput , strlen(pinput) - 1);
        memset(&input,0, sizeof(input));
    //***************************************************** I.D GEN
    
    
        srand(time(NULL));
        int r, id1, id2;
    
        do{
            r = rand();
        }while(r>100);
    
        do {
            id1 = rand() / r ;
            }while(id1<1000 || id1 >10000);
        do {
            id2 = rand() / 2 - r;
                }while(id2<10000 || id2 >100000);
    
            printf("Your I.D is: %d-%d \n", id1, id2);
    
        printf("Please retype I.D to comfirm: \n");
        pinput = fgets(input, BUFF, stdin);
        strncpy(pinput ,(pct + *pctcounter-1) ->ID , strlen(pinput) - 1);
        memset(&input,0, sizeof(input));
    
    
        //****************
    
    //*****************************************************
         system("cls");
          printf("-------------------------------\n");
    
    
           printf("client lastname\t: %s\n", (pct + *pctcounter-1)->firstName);
          printf("client lastname\t: %s\n", (pct + *pctcounter-1)-> lastName);
          printf("client emai\t: %s\n", (pct + *pctcounter-1)-> email);
          printf("client ID\t: %s\n", (pct + *pctcounter-1)-> ID);
          printf("client funds\t: %.02f\n", (pct + *pctcounter-1)-> funds);
    
    
        return  pinput;
    }
    //****************************************************************
    char checkNameTypeA(char *pinput)
    {
        int i = 0;
        for( i = 0; i < strlen(pinput) - 1; i++)
        {
            if(!isalpha(*(pinput+i)))
            {
                return 0;
            }
    
        }
        return 1;
    }
    //******************************************
    int checkCurr(char *pinput)
    {
        int i = 0;
        int isNotVal=0;
        for(i=strlen(pinput) - 1;i>0 - 1; i--)
        {
            if(i==strlen(pinput)-4)
            {
               if((int)(pinput+i)==46)
               {
    
               }
               else
               {
                    isNotVal = 1;
    
               }
    
            }else{
                    if((int)(pinput+i)>=48&&(int)(pinput+i)<=57)
                    {
    
                    }
                    else
                        isNotVal = 1;
                }
        }
        return isNotVal;
    }
    I would simply like it to accept the info provided in the fields and i still haven't exactly worked out how to store the different instances of the client struct. I suppose a separate array. I started on it but I am lost in my own code at this point... Again sorry for the lack of clarity.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The warning I get.

    Tim S.

    Code:
    V:\SourceCode\Projects\ctest\main.c||In function 'getct':|
    V:\SourceCode\Projects\ctest\main.c|103|warning: passing argument 1 of 'fgets' from incompatible pointer type [enabled by default]|
    c:\program files\codeblocks_12_11\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\stdio.h|354|note: expected 'char *' but argument is of type 'char **'|
    V:\SourceCode\Projects\ctest\main.c|110|warning: passing argument 1 of 'fgets' from incompatible pointer type [enabled by default]|
    c:\program files\codeblocks_12_11\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\stdio.h|354|note: expected 'char *' but argument is of type 'char **'|
    V:\SourceCode\Projects\ctest\main.c|115|warning: passing argument 1 of 'fgets' from incompatible pointer type [enabled by default]|
    c:\program files\codeblocks_12_11\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\stdio.h|354|note: expected 'char *' but argument is of type 'char **'|
    V:\SourceCode\Projects\ctest\main.c|138|warning: passing argument 1 of 'fgets' from incompatible pointer type [enabled by default]|
    c:\program files\codeblocks_12_11\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\stdio.h|354|note: expected 'char *' but argument is of type 'char **'|
    V:\SourceCode\Projects\ctest\main.c|157|warning: return makes integer from pointer without a cast [enabled by default]|
    V:\SourceCode\Projects\ctest\main.c||In function 'checkNameTypeA':|
    V:\SourceCode\Projects\ctest\main.c|165|warning: implicit declaration of function 'isalpha' [-Wimplicit-function-declaration]|
    ||=== Build finished: 0 errors, 6 warnings (0 minutes, 2 seconds) ===|
    Likely wrong
    Code:
    char * input[BUFF];
    Likely right
    Code:
    char input[BUFF];
    Edit: I have no idea what getct is supposed to return; whatever it is, it is NOT being done right.
    Last edited by stahta01; 03-31-2013 at 08:49 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User Alexander87's Avatar
    Join Date
    Mar 2013
    Location
    Jacksonville, Florida, United States
    Posts
    11
    Quote Originally Posted by stahta01 View Post
    The warning I get.

    Tim S.

    Code:
    V:\SourceCode\Projects\ctest\main.c||In function 'getct':|
    V:\SourceCode\Projects\ctest\main.c|103|warning: passing argument 1 of 'fgets' from incompatible pointer type [enabled by default]|
    c:\program files\codeblocks_12_11\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\stdio.h|354|note: expected 'char *' but argument is of type 'char **'|
    V:\SourceCode\Projects\ctest\main.c|110|warning: passing argument 1 of 'fgets' from incompatible pointer type [enabled by default]|
    c:\program files\codeblocks_12_11\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\stdio.h|354|note: expected 'char *' but argument is of type 'char **'|
    V:\SourceCode\Projects\ctest\main.c|115|warning: passing argument 1 of 'fgets' from incompatible pointer type [enabled by default]|
    c:\program files\codeblocks_12_11\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\stdio.h|354|note: expected 'char *' but argument is of type 'char **'|
    V:\SourceCode\Projects\ctest\main.c|138|warning: passing argument 1 of 'fgets' from incompatible pointer type [enabled by default]|
    c:\program files\codeblocks_12_11\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\stdio.h|354|note: expected 'char *' but argument is of type 'char **'|
    V:\SourceCode\Projects\ctest\main.c|157|warning: return makes integer from pointer without a cast [enabled by default]|
    V:\SourceCode\Projects\ctest\main.c||In function 'checkNameTypeA':|
    V:\SourceCode\Projects\ctest\main.c|165|warning: implicit declaration of function 'isalpha' [-Wimplicit-function-declaration]|
    ||=== Build finished: 0 errors, 6 warnings (0 minutes, 2 seconds) ===|
    Likely wrong
    Code:
    char * input[BUFF];
    Likely right
    Code:
    char input[BUFF];
    Edit: I have no idea what getct is supposed to return; whatever it is, it is NOT being done right.
    I took it off because I knew I needed to change it however... I didn't know to what so I suppose I must have forgot about it. I would like it to accept the struct fields properly .However, I am having no such luck it crashes when it gets to the end of collecting data and something it stops on generating the ID... o.0

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You really need to sit down and study pointers.

    char * input[BUFF]; where BUFF is 120.

    This creates 120 pointers that don't point anywhere.

    Try this video: Binky Pointer Fun Video

    A 1 dimensional array of something (like char) can be automatically converted to a pointer by referring to it's name. So the "likely right" example Tim gave

    char input[BUFF];

    can become a pointer if you just type input, in all but three contexts.

    When you understand that, come back to code like this

    Code:
    memset(&input, 0, sizeof(input));
    printf("Enter your firstname: ");
    pinput = fgets(pinput, BUFF, stdin);
    strncpy((pct + *pctcounter-1) ->firstName,pinput , strlen(pinput) - 1);
    and straighten it out by making sure all of the accesses you're requesting make sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers passed as argument to fgets
    By shruthi in forum C Programming
    Replies: 16
    Last Post: 09-07-2012, 11:12 PM
  2. Pointers make sense
    By Flatpackvicar in forum C++ Programming
    Replies: 2
    Last Post: 07-14-2012, 08:54 AM
  3. fgets with pointers?
    By petz_e in forum C Programming
    Replies: 5
    Last Post: 11-16-2011, 03:21 PM
  4. How do i make an array of pointers for execvp
    By Blasz in forum C Programming
    Replies: 4
    Last Post: 05-18-2010, 10:42 PM