Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do I have an incompatible pointer type in C?
    primarykey
    data
    text
    <p>I have to write a program for my C programming that reads the strings into an typedef structure utilizing a dynamic array for it's storage, then calculate the amount of vowels and consonants in names, the vowels are ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ and ‘y’. The character – (‘-‘) is neither consonant nor vowel. Then I need to update the data adding 3 years of age to each one and print out the data. Everything must be in separate functions.</p> <ul> <li>Calculate the amount of vowels and consonants in all 7 names</li> <li>Must be created using typedef structure and dynamically allocated array</li> <li>Function later on should be updated by adding 3 years to each profile and printed with the end results</li> </ul> <p>The data that I need to analyze is this:</p> <p>Fred Flintstone38Male</p> <p>Barney Rubble36Male</p> <p>Wilma Flintstone37Female</p> <p>Betty Rubble36Female</p> <p>Pebbles Flintstone4Female</p> <p>Bam-Bam Rubble3Male</p> <p>Dino Flintstone2Male</p> <p>This is what I've got so far, but program keeps telling me that I have an incompatible pointer types, and I cannot figure out where is the mistake.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;ctype.h&gt; typedef struct { char name[20]; int age[2]; char gender[6]; }CARTOON; // Function Prototype Declarations int printit(CARTOON *, FILE *); int loaddata(CARTOON *, int, FILE *, FILE *); void countVowel (char **pNames, int *vowel, int *consonants); void printResults (int vowel, int consonants); // Main int main (void) { char input[40]; int namesIndex; int vowel; int consonants; CARTOON* pNames; FILE *tptr; FILE *bptr; FILE *fptr; //reading in the file "data.txt" if ((fptr=fopen("data.txt", "r"))==NULL) { printf("Error opening data.txt\n"); return -1; } if((bptr=fopen("lab5dat.bin","wb"))==NULL) { printf("Error creating lab4.bin\n"); return 2; } pNames =(char**)calloc(8,sizeof(CARTOON)); namesIndex = 0; while (namesIndex &lt; 8 &amp;&amp; fgets(input,40,fptr)) { *(pNames + namesIndex)=(char*) calloc(strlen(input)+1, sizeof(char));//pointer to first string in array namesIndex strcpy(*(pNames + namesIndex), input); namesIndex++; } *(pNames + namesIndex)=NULL; namesIndex = 0; pNames=(CARTOON*) calloc(12,sizeof(CARTOON)); loaddata(pNames, namesIndex, tptr, bptr); fclose(tptr); fclose(bptr); if((bptr=fopen("lab5dat.bin","rb"))==NULL) { printf("Error opening lab5dat.bin\n"); return 3; } //printit(pNames, bptr); // Prints the items in the "lab3.dat" file. namesIndex=0; while (*(pNames + namesIndex)) { printf("%s\n", *(pNames + namesIndex)); namesIndex++; } // Calls function countVowel countVowel (pNames, &amp;vowel, &amp;consonants); //Calls function printResults printResults(vowel, consonants); system("pause"); return 0; } //end int printit(CARTOON *pNames, FILE *bptr) { int num; printf("Data from print \n\n"); //num=fread(pNames, sizeof(CARTOON), 1, bptr); while (!(feof(bptr))) { num=fread(pNames, sizeof(CARTOON), 1, bptr); printf("%s %d %s \n",pNames-&gt;name,pNames-&gt;age,pNames-&gt;gender); } return 0; } int loaddata( CARTOON *pNames, int namesIndex, FILE *tptr, FILE *bptr) { printf("Data from loaddata\n\n"); while (!(feof(tptr))) { fgets((pNames + namesIndex)-&gt;name,20,tptr); fscanf(tptr,"%d",&amp;pNames[namesIndex].age); fgets((pNames + namesIndex)-&gt;gender,18,tptr); fwrite((pNames + namesIndex),sizeof(CARTOON),1,bptr); printf("%s\n", (pNames + namesIndex)-&gt;name); namesIndex++; } return 0; } //function countVowel will count the number of vowels and consonants void countVowel (char **pNames, int *vowel, int *consonants) { int namesIndex; int stringIndex; namesIndex=0; stringIndex=0; *vowel=0; *consonants=0; while(*(pNames + namesIndex)) { stringIndex=0; while(stringIndex&lt;strlen(*(pNames + namesIndex))) { if (isalpha (pNames[namesIndex][stringIndex] )) //Reads only alphabets switch (toupper(pNames[namesIndex][stringIndex])) //makes everything capitalized. { case 'A': (*vowel)++ ;break; //*vowel count gets incremented by 1 whenever a vowel is found. case 'E': (*vowel)++ ;break; case 'I': (*vowel)++ ;break; case 'O': (*vowel)++ ;break; case 'U': (*vowel)++ ;break; case 'Y': (*vowel)++ ;break; default: (*consonants)++;break; // Everything that is not a vowel increments consonants by 1. } stringIndex++; // goes to the next index in the string } namesIndex++; //goes to the next array index, when end of string is reached } } //Prints the result of the number of vowels and consonants void printResults (int vowel, int consonants) { printf ("\n\nThere are %d vowels and %d consonants\n\n", vowel, consonants); } </code></pre> <p>Right now the error is between those two lines of codes</p> <pre><code>*(pNames + namesIndex)=(char*) calloc(strlen(input)+1, sizeof(char));//pointer to first string in array namesIndex strcpy(*(pNames + namesIndex), input); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload