Thread: Pointer to print name and price

  1. #1
    Registered User
    Join Date
    Aug 2015
    Location
    India
    Posts
    14

    Question Pointer to print name and price

    C program Using Pointer to print the name and price of the items sold in a retail shop on a specific date.
    I am unable to understand this question will somebody explain how can i program code for this. I know how to print name and price of the item using pointer but i am bit confuse "specific date" what's the exactly means? is it mean user will input a specific date and bunch of data will appear on his screen? so do i need to use any "Search Algorithm" ?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    is it mean user will input a specific date and bunch of data will appear on his screen?
    Probably.
    so do i need to use any "Search Algorithm" ?
    Yes. Have you tried anything?

  3. #3
    Registered User
    Join Date
    Aug 2015
    Location
    India
    Posts
    14
    Not Yet... I will come back with codes

  4. #4
    Registered User
    Join Date
    Aug 2015
    Location
    India
    Posts
    14
    Code:
    #include<stdio.h>
    #define SIZE 3
    main()
    {
        char item[SIZE][100];
        char *pi[SIZE];
        float price[SIZE];
        float *pp[SIZE];
        int date[SIZE],i, sea;
        int *pd[SIZE];
        printf("\t******************************\n\n");
        printf("\t\tMonth: December\n\t\tYear : 2015\n\n");
        printf("\t******************************\n\n");
    
    
        for(i=0; i<SIZE; i++)
        {
                printf("\nEnter Date: ");
                scanf("%d", &date[i]);
                pd [i] = &date[i];
                printf("Enter Item Name: ");
                scanf("%s", &item[i]);
                pi [i] = &item[i];
                printf("Enter Price: ");
                scanf("%f", &price[i]);
                pp [i] = &price[i];
        }
        printf("\nEnter Date for search: ");
        scanf("%d", &sea);
        printf("\n**************************\n");
        for(i=0; i<SIZE; i++)
        {
            if(sea == *pd[i])
            {
                printf("\nItem Name: %s\nPrice    : %0.2f\n", pi[i], *pp[i]);
            }
        }
    }
    Do you think I am going in correct path ?

  5. #5
    Registered User
    Join Date
    Oct 2015
    Location
    Turkey
    Posts
    10
    It looks like it's correct I think but maybe you need to change
    Code:
    pi [i] = &item[i];
    to
    Code:
    pi [i] = item[i];
    to don't get warning. It will be work fine but it can give you a warning message.
    Last edited by sulfur93; 11-29-2015 at 10:23 AM. Reason: spelling

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm not sure what all the extra pointer arrays are supposed to do. Stuff like float *pp, char *pi, int *pd... you don't need these things. It looks like you're adding in layers of indirection for no good reason.

    For example, you could just compare sea and date[i]:
    Code:
    if (sea == date[i])

  7. #7
    Registered User
    Join Date
    Aug 2015
    Location
    India
    Posts
    14
    This is just because Question is demanding me to use pointer for printing all those stuffs.

  8. #8
    Registered User
    Join Date
    Aug 2015
    Location
    India
    Posts
    14
    I got it thanks sulfur93
    Last edited by raj21; 11-30-2015 at 12:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-13-2012, 12:34 PM
  2. Getting error while trying to print out a char pointer
    By sigur47 in forum C Programming
    Replies: 1
    Last Post: 05-05-2012, 05:46 AM
  3. how can i print a pointer?
    By nullifyed in forum C Programming
    Replies: 5
    Last Post: 06-03-2010, 01:48 AM
  4. how to print pointer variable
    By unikgila in forum C Programming
    Replies: 11
    Last Post: 10-27-2008, 09:49 AM
  5. Replies: 2
    Last Post: 08-05-2002, 04:52 PM

Tags for this Thread