0

Here I'm trying to write a c programming below,Here I created a data type person which has two arguments name and number (creating a name of the candidate and votes they have recieved), and when prompt for input,if user enters the name in the array of person data_type and number corresponds to that number should increase,but I got stuck in this error below.

#include<stdio.h>
#include<cs50.h>
#include<string.h>
//Making person variable
typedef struct
{
    string name;
    int number;

}
person;

int update_vote(string name,person arr);

int main(void)
{
    person cand[4];
    cand[0].name="Brian";
    cand[1].name="David";
    cand[2].name="Obama";
    cand[3].name="Biden";
    
    cand[0].number=0;
    cand[1].number=0;
    cand[2].number=0;
    cand[3].number=0;
    
    //print the candidate names on the screen
    float len=sizeof(cand)/sizeof(cand[0]);
    int yu =(int) len;
    printf("Candidates Who are participating:");
    printf("\n");
    for (int i=0;i<yu;i++)
    {
      printf("%i.%s ",i+1,cand[i].name); 
    }
    printf("\n");
    
    //prompt for voters and take votes and update
    int voters=get_int("Enter the number of voters:");
    
    for (int j=0;j<voters;j++)
    {
        string vote=get_string("Enter your vote:");
        int update=update_vote(vote,cand);
        if (update!=-1)
        {
            cand[update].number++;
        }
        else{
            printf("Invalid name entered");
        }
    }
    
    printf("%d",cand);
    
}

int update_vote(string name,person arr)
{
     float len=sizeof(arr)/sizeof(arr[0]);
     int yu =(int) len;
     for(int i=0;i<yu;i++)
     {
         if (strcmp((name,arr[i].name)==0))
         {
             return i;
         }
     }
     return -1;
}

But got stuck in figuring out why the error is occuring below

error: passing 'person [4]' to parameter of incompatible type 'person' int update=update_vote(vote,cand);

Omkar
  • 59
  • 1
  • 9

2 Answers2

2

The error is because you are passing person* where person is expected as an argument.

You should change the type of argument so that it can receive (a pointer to) an array.

Also note that sizeof cannot be used to determing the number of elements of arrays passed as arguments. You should pass the number of elements separately.

Another point is that printf("%d",cand); will invoke undefined behavior because data having wrong type is passed. %d expects int. If you want to print a pointer, you should cast it to void* and use %p format specifier.

Try this:

#include<stdio.h>
#include<cs50.h>
#include<string.h>
//Making person variable
typedef struct
{
    string name;
    int number;

}
person;

int update_vote(string name,person* arr,int arrSize); /* fix arguments */

int main(void)
{
    person cand[4];
    cand[0].name="Brian";
    cand[1].name="David";
    cand[2].name="Obama";
    cand[3].name="Biden";
    
    cand[0].number=0;
    cand[1].number=0;
    cand[2].number=0;
    cand[3].number=0;
    
    //print the candidate names on the screen
    float len=sizeof(cand)/sizeof(cand[0]);
    int yu =(int) len;
    printf("Candidates Who are participating:");
    printf("\n");
    for (int i=0;i<yu;i++)
    {
      printf("%i.%s ",i+1,cand[i].name); 
    }
    printf("\n");
    
    //prompt for voters and take votes and update
    int voters=get_int("Enter the number of voters:");
    
    for (int j=0;j<voters;j++)
    {
        string vote=get_string("Enter your vote:");
        int update=update_vote(vote,cand,yu); /* pass the number of elements */
        if (update!=-1)
        {
            cand[update].number++;
        }
        else{
            printf("Invalid name entered");
        }
    }
    
    printf("%p",(void*)cand); /* use correct way to print a pointer */
    
}

int update_vote(string name,person* arr,int arrSize) /* fix arguments */
{
     int yu =arrSize; /* use the passed size instead of sizeof */
     for(int i=0;i<yu;i++)
     {
         if (strcmp((name,arr[i].name)==0))
         {
             return i;
         }
     }
     return -1;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • could you plz explain the cause of the problem,if you don't mind. – Omkar Jul 27 '21 at 15:39
  • @Omkar I already explained that: the type of the argument `arr` is wrong. – MikeCAT Jul 27 '21 at 15:42
  • yes but I didn't got what you told, Is it because we should use * symbol while using a data type which was created locally within that program? – Omkar Jul 27 '21 at 15:45
  • @Omkar No. Generally an argument having type `some_type` cannot receive an array of `some_type`. Arrays are (usually) passed as a pointer to their first element in C. Threfore you will need an argument of a pointer `some_type*`. – MikeCAT Jul 27 '21 at 15:47
  • Is it possible to print entire array of person data_type without the usage of FOR loop – Omkar Jul 27 '21 at 16:03
  • @Omkar Yes, using `while` loop. `for (A; B; C) D` is (almost) equivalent to `A; while(B) {D C; }`. – MikeCAT Jul 27 '21 at 16:04
  • yes but without usage of any loop,can we print entire array using printf only – Omkar Jul 27 '21 at 16:07
  • @Omkar If the number of elements is known, you can do that by giving all things to print like `printf("%s %d\n%s %d\n%s %d\n%s %d\n", cand[0].name, cand[0].number, cand[1].name, cand[1].number, cand[2].name, cand[2].number, cand[3].name, cand[3].number);` – MikeCAT Jul 27 '21 at 16:12
0

Because person cand[4]; , when you use " int update=update_vote(vote,cand);" , it means 'person *' that it's pointer.

Y.Jie
  • 148
  • 1
  • 8