Question

C programming How to get rid of the below error. I do not know why is...

C programming

How to get rid of the below error. I do not know why is this happening and how can I correct it. Please help.

typedef struct circleroute{
char* name;
} Circle;

Circle* circlemalloc(int nroutes); //error free
Circle* readRoute(FILE* fin); //error free

int main(int argc, char* argv[]){
if(argc < 2){
if(argv[1] != NULL){
FILE *fin = fopen(argv[1], "r");
if(fin == NULL){
printf("Unable to open file %s!\n", argv[1]);
return EXIT_FAILURE;
}
int routes;
fscanf(fin, "%d", &routes);
Circle* c1= circlemalloc(routes);
for(int i = 0; i < routes; i++){
c1[i] = readRoute(fin); //This gives me an error saying incompatible types when assigning to type Circle from type struct Circle *
}
}
}
}

Circle* circlemalloc(int nroutes){
Circle* cThis = (Circle*)malloc(sizeof(Circle) * nroutes);
   return cThis;
}

Circle* readRoute(FILE* fin){
Circle* obj;
int val;
char* rName;
fscanf(fin, "%d", &val);
fscanf(fin, "%s", rName);
obj->name = rName;
return obj;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Your c1 points to Array of Pointer to Circle. So, it should be declared as Circle**. Circle* can point to only one

Circle object.

Please try the following code

#include <stdio.h>
#include <malloc.h>

typedef struct circleroute {
char* name;
} Circle;

Circle** circlemalloc(int nroutes); //error free
Circle* readRoute(FILE* fin); //error free

int main(int argc, char* argv[]) {
if (argc < 2) {
if (argv[1] != NULL) {
FILE* fin = fopen(argv[1], "r");
if (fin == NULL) {
printf("Unable to open file %s!\n", argv[1]);
return EXIT_FAILURE;
}
int routes;
fscanf(fin, "%d", &routes);
Circle** c1 = circlemalloc(routes);
for (int i = 0; i < routes; i++) {
c1[i] = readRoute(fin); //This gives me an error saying incompatible types when assigning to type Circle from type struct Circle *
}
}
}
}

Circle** circlemalloc(int nroutes) {
Circle** cThis = (Circle**)malloc(sizeof(Circle) * nroutes);
return cThis;
}
Circle* readRoute(FILE* fin) {
Circle* obj;
int val;
char* rName;
fscanf(fin, "%d", &val);
fscanf(fin, "%s", rName);
obj->name = rName;
return obj;
}

Add a comment
Know the answer?
Add Answer to:
C programming How to get rid of the below error. I do not know why is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • My question is listed the below please any help this assignment ; There is a skeleton...

    My question is listed the below please any help this assignment ; There is a skeleton code:  copy_file_01.c #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { char ch ; FILE *source , *target;    if(argc != 3){ printf ("Usage: copy file1 file2"); exit(EXIT_FAILURE); } source = fopen(argv[1], "r"); if (source == NULL) { printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } target = fopen(argv[2], "w"); if (target == NULL) { fclose(source); printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } while ((ch...

  • I am having problems with the following assignment. It is done in the c language. The...

    I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...

  • Modify the client server system program given below so that instead of sendto() and recvfrom(), you...

    Modify the client server system program given below so that instead of sendto() and recvfrom(), you use connect() and un-addresssed write() and read() calls. //Server.c #include #include #include #include #include #include #include #include #include #include # define PortNo 4567 # define BUFFER 1024 int main(int argc, char ** argv) { int ssd; int n; socklen_t len; char msg[BUFFER]; char clientmsg[BUFFER]; struct sockaddr_in server; struct sockaddr_in client; int max_iterations = 0; int count = 0, totalChar = 0, i = 0;...

  • Question 11 pts Fill in the blank. Two functions are defined main.c and MyAge.c   // FILE...

    Question 11 pts Fill in the blank. Two functions are defined main.c and MyAge.c   // FILE main.c main ( ) { _______ int age ; printf ( " the value of age is %d \n", age ) ; } // FILE MyAge.c int age = 10; int MyAge ( ) { } Group of answer choices static external internal extern Flag this Question Question 21 pts Fill in the blank. Two functions are defined main.c and MyAge.c   The below program...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • Help with my code: The code is suppose to read a text file and when u...

    Help with my code: The code is suppose to read a text file and when u enter the winning lotto number it suppose to show the winner without the user typing in the other text file please help cause when i enter the number the code just end without displaying the other text file #include <stdio.h> #include <stdlib.h> typedef struct KnightsBallLottoPlayer{ char firstName[20]; char lastName[20]; int numbers[6]; }KBLottoPlayer; int main(){ //Declare Variables FILE *fp; int i,j,n,k; fp = fopen("KnightsBall.in","r"); //...

  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT