Thread: neither array nor pointer

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    5

    neither array nor pointer

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    /*symbolic constant definition*/
    #define L 31
    #define SIZE 4
    #define M 3
    #define UCOST 1.50
    /*structure definition*/
    typedef struct
    {
       char name[L];
       int numm[M];
       float tcost;
    }astaff;
    /*function prototypes*/
    void Input(astaff *prec);
    void Display(astaff *prec);
    void SearchByPartialSName(astaff rec[]);
    void SortBySName(astaff *prec);
    int main(void)
    {
       astaff rec[SIZE];
       Input(&rec[0]);/*function call*/
       Display(&rec[0]);/*function call*/
       SearchByPartialSName(rec);/*function call*/
       SortBySName(&rec[0]);/*function call*/
       if(!getch())getch();
       return 0;
    }
    /*function definition*/
    void Input (astaff *prec)
    {
       int i,j, tmarker;
       char mcolor[M][6]= {"BLACK","BLUE","RED"};
       for(i = 0; i <= SIZE; ++i, ++prec)
    
          {
             printf("***ACADEMIC STAFF # %d DATA ENTRY***\n", i+1);
             printf("NAME: "); scanf(" %[^\n]", prec->name); strupr(prec->name);
             printf("=>NUMBER OF WHITEBOARD MARKERS REQUESTED<=\n");
             for(j = 0, tmarker = 0; j <= M; ++j)
             {
                printf("%s MARKERS:(0-5): ", mcolor[j]);
                scanf("%d", &prec->numm[j]);
                tmarker += prec->numm[j];
             }
             prec->tcost[i] = tmarker * UCOST;
             printf("=>TOTAL COST = RM \n", &prec->tcost[i]);
          }
       return;
    }
    void Display (astaff *prec)
    {
       int i,j, marker;
       char mcolor[M][6]= {"BLACK", "BLUE", "RED"};
       printf("%2s %-30s\n", "NO", "STAFFNAME");
       for(j = 0; j < M; ++j)
          printf("%5s", mcolor[j]);
       printf("%15s\n", "TOTALCOST(RM)");
       for(i = 0; i < SIZE; ++i)
       {
          printf("%2d %-30s", i+1, (prec+i)->name);
          for(j = 0; j < M; ++j)
             printf("%5d ", (prec+i)->numm[j]);
          printf("%15.2f\n", (prec+i)->tcost);
       }
       return ;
    }
    void SearchByPartialSName(astaff rec[])
    {
       int i,j;
       char input[L];
       printf("Enter a staff name to search: ");
       scanf(" %[^\n]", input); strupr(input);
       printf("%-30s %-6s %-6s %-6s %-6s %-16s\n",
                    "STAFFNAME", "BLACK", "BLUE", "RED", "TOTALCOST");
       for(i = 0; i < SIZE; ++i)
          if(strstr(rec[i].name, input) != NULL)
          {
             for(j = 0; j < M; ++j)
             printf("%-30s %5d %5d %3d %6c %5.1f\n",
                    rec[i].name, rec[i].numm[j], rec[i].tcost);
          }
       if (i == SIZE) printf("Academic staff name not found!");
       return;
    }
    void SortBySName(astaff *prec)
    {
       int i, pass;
       astaff temp;
       for(pass =1; pass < SIZE; ++pass)
          for(i=0; i < SIZE -1;++i)
             if(strcmp(prec[i].name,prec[i+1].name)>0)
             {
                temp = prec[i];
                prec[i]= prec[i+1];
                prec[i+1] = temp;
             }
       return ;
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    It always give me error message which is neither array nor pointer in prec->tcost[i] = tmarker * UCOST; and printf("=>TOTAL COST = RM \n", &prec->tcost[i]); part. please help me.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Is there a reason that you're mixing pointer style notation and array notation?

    Code:
            prec->tcost[i] = tmarker * UCOST;
    In the above snippet prec is an array so why not use array[] notation instead of the *pointer notation? And remember tcost is not an array it is a single variable.

    The same code using array notation, note not sure 'i' is the proper value for the index.
    Code:
            prec[i].tcost = tmarker * UCOST;
    Also note you may have possible buffer overruns in several places. Look at the following:
    Code:
    for(i = 0; i <= SIZE; ++i, ++prec)
    This looks like you will access your arrays out of bounds.

  4. #4
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    My lecture request for using mixing pointer style notation and array notation

  5. #5
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    Quote Originally Posted by jimblumberg View Post
    Is there a reason that you're mixing pointer style notation and array notation?

    Code:
            prec->tcost[i] = tmarker * UCOST;
    In the above snippet prec is an array so why not use array[] notation instead of the *pointer notation? And remember tcost is not an array it is a single variable.

    The same code using array notation, note not sure 'i' is the proper value for the index.
    Code:
            prec[i].tcost = tmarker * UCOST;
    Also note you may have possible buffer overruns in several places. Look at the following:
    Code:
    for(i = 0; i <= SIZE; ++i, ++prec)
    This looks like you will access your arrays out of bounds.
    My lecture request for using mixing pointer style notation and array notation

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    My lecture request for using mixing pointer style notation and array notation
    Then you need to remember what is using pointer notation and what is using array notation and what is a single variable and then use the proper notation. And IMO mixing the two is an excellent road to confusion.

    Code:
    prec->tcost = tmarker * UCOST;

  7. #7
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    Quote Originally Posted by jimblumberg View Post
    Then you need to remember what is using pointer notation and what is using array notation and what is a single variable and then use the proper notation. And IMO mixing the two is an excellent road to confusion.

    Code:
    prec->tcost = tmarker * UCOST;
    Ok. Thanks for your answer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing 2D array to function via pointer of pointer
    By sean_cantab in forum C Programming
    Replies: 4
    Last Post: 05-09-2016, 10:15 AM
  2. Replies: 16
    Last Post: 01-28-2010, 02:44 AM
  3. pointer to pointer that points to a char array
    By steve1_rm in forum C Programming
    Replies: 2
    Last Post: 01-14-2009, 12:03 AM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. A pointer to a character pointer array... can't pass
    By Lynux-Penguin in forum C Programming
    Replies: 9
    Last Post: 10-12-2003, 10:53 PM

Tags for this Thread