Pointer conversion

F

fb

Hello. I have this program that I copied out of a textbook. I can't
seem to get it to work. It's a rather old book, that seems to be using
old K&R C. I fixed up to be more standardized, but I still get a
"nonportable pointer conversion in function initialize" error. I looked
at the initialize function, but it seemed ok. So then I checked the
caller, but that looked ok too... I am rather new at C programming and
pointers (and arrays and..... etc).

/* Convert English into Piglatin */

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

/* Function Prototypes */

void initialize(char english[], char piglatin[]);
void readinput(char english[]);
int countwords(char english[]);
void convert(int words, char english[], char piglatin[]);
void writeoutput(char piglatin[]);

int main(void)
{
char english[80], piglatin[80];
int words;

printf("Welcome to the Piglatin Generator\n\n");
printf("Type \'END\' when finished\n\n");

do { /* process a new line of text */
initialize(english, piglatin);
readinput(english);

/* test for stopping condition */
if (toupper(english[0]) == 'E' &&
toupper(english[1]) == 'N' &&
toupper(english[2]) == 'D')
break;

/* Count the number of words in the line */
words = countwords(english);

/* Convert english into piglatin */
convert(words, english, piglatin);
writeoutput(piglatin);
}
while (words >= 0);

printf("\naveha aa icena ayda (Have a nice day)\n");

return EXIT_SUCCESS;
}

/* initialize the character arrays with blank spaces */

void initialize(char english[], char piglatin[])
{
int count;

for (count = 0; count < 80; count++)
english[count] = piglatin[count] = " ";
return;
}

/* read one line of English text */

void readinput(char english[])
{
int count = 0;
char c;

while ((c = getchar()) != '\n') {
english[count] = c;
count++;
}
return;
}

/* scan the English text and determine the number of words */
int countwords(char english[])
{
int count, words = 1;

for(count = 0; count < 79; count++)
if (english[count] == ' ' && english[count + 1] != ' ')
words++;
return(words);
}

/* convert each word into piglatin */

void convert(int words, char english[], char piglatin[])
{
int n, count;
int m1 = 0; /* marker -> beginning of word */
int m2; /* marker -> end of word */

/* convert each word */
for (n = 1; n <= words; n++) {

/* locate the end of the current word */
count = m1;
while (english[count] != ' ')
m2 = count++;

/* transpose the first letter and add 'a' */
for (count = m1; count < m2; count++)
piglatin[count + (n - 1)] = english[count + 1];
piglatin[m2 + (n - 1)] = english[m1];
piglatin[m2 + n] = 'a';

/* reset the initial marker */
m1 = m2 + 2;
}
return;
}

/* display the line of text in piglatin */
void writeoutput(char piglatin[])
{
int count = 0;

for(count = 0; count < 80; count++)
putchar(piglatin[count]);
printf("\n");
return;
}
 
A

Artie Gold

fb said:
Hello. I have this program that I copied out of a textbook. I can't
seem to get it to work. It's a rather old book, that seems to be using
old K&R C. I fixed up to be more standardized, but I still get a
"nonportable pointer conversion in function initialize" error. I looked
at the initialize function, but it seemed ok. So then I checked the
caller, but that looked ok too... I am rather new at C programming and
pointers (and arrays and..... etc).

/* Convert English into Piglatin */

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

/* Function Prototypes */

void initialize(char english[], char piglatin[]);
void readinput(char english[]);
int countwords(char english[]);
void convert(int words, char english[], char piglatin[]);
void writeoutput(char piglatin[]);

int main(void)
{
char english[80], piglatin[80];
int words;

printf("Welcome to the Piglatin Generator\n\n");
printf("Type \'END\' when finished\n\n");

do { /* process a new line of text */
initialize(english, piglatin);
readinput(english);

/* test for stopping condition */
if (toupper(english[0]) == 'E' &&
toupper(english[1]) == 'N' &&
toupper(english[2]) == 'D')
break;

/* Count the number of words in the line */
words = countwords(english);

/* Convert english into piglatin */
convert(words, english, piglatin);
writeoutput(piglatin);
}
while (words >= 0);

printf("\naveha aa icena ayda (Have a nice day)\n");

return EXIT_SUCCESS;
}

/* initialize the character arrays with blank spaces */

void initialize(char english[], char piglatin[])
{
int count;

for (count = 0; count < 80; count++)
english[count] = piglatin[count] = " ";
ITYM:
english[count] = piglatin[count] = ' ';

Remember, " " is a literal string (yielding a *pointer* to its first
element), but ' ' is a char.
return;
}

/* read one line of English text */

void readinput(char english[])
{
int count = 0;
char c;

while ((c = getchar()) != '\n') {
english[count] = c;
count++;
}
return;
}

/* scan the English text and determine the number of words */
int countwords(char english[])
{
int count, words = 1;

for(count = 0; count < 79; count++)
if (english[count] == ' ' && english[count + 1] != ' ')
words++;
return(words);
}

/* convert each word into piglatin */

void convert(int words, char english[], char piglatin[])
{
int n, count;
int m1 = 0; /* marker -> beginning of word */
int m2; /* marker -> end of word */

/* convert each word */
for (n = 1; n <= words; n++) {

/* locate the end of the current word */
count = m1;
while (english[count] != ' ')
m2 = count++;

/* transpose the first letter and add 'a' */
for (count = m1; count < m2; count++)
piglatin[count + (n - 1)] = english[count + 1];
piglatin[m2 + (n - 1)] = english[m1];
piglatin[m2 + n] = 'a';

/* reset the initial marker */
m1 = m2 + 2;
}
return;
}

/* display the line of text in piglatin */
void writeoutput(char piglatin[])
{
int count = 0;

for(count = 0; count < 80; count++)
putchar(piglatin[count]);
printf("\n");
return;
}
HTH,
--ag
 
M

Martin Ambuhl

fb said:
Hello. I have this program that I copied out of a textbook. I can't
seem to get it to work. It's a rather old book, that seems to be using
old K&R C. I fixed up to be more standardized, but I still get a
"nonportable pointer conversion in function initialize" error. I looked
at the initialize function, but it seemed ok. So then I checked the
caller, but that looked ok too... I am rather new at C programming and
pointers (and arrays and..... etc).

I am not going to waste bandwidth reposting the original code (available
in message <6_8_c.314355$gE.280449@pd7tw3no>). Here are two corrections
you need to make:

void initialize(char english[], char piglatin[])
{
int count;

for (count = 0; count < 80; count++)
english[count] = piglatin[count] = ' '; /* mha: changed strings
" " to the char ' ' */
[ ... ]
void convert(int words, char english[], char piglatin[])
{
int n, count;
int m1 = 0; /* marker -> beginning of word */
int m2 = 0; /* mha: forced m2 to be initialized */
 
C

CBFalconer

fb said:
Hello. I have this program that I copied out of a textbook. I
can't seem to get it to work. It's a rather old book, that seems
to be using old K&R C. I fixed up to be more standardized, but I
still get a "nonportable pointer conversion in function
initialize" error. I looked at the initialize function, but it
seemed ok. So then I checked the caller, but that looked ok
too... I am rather new at C programming and pointers (and arrays
and..... etc).
.... snip ...

/* initialize the character arrays with blank spaces */

void initialize(char english[], char piglatin[])
{
int count;

for (count = 0; count < 80; count++)
english[count] = piglatin[count] = " ";
return;
}

" " is a string represented by a pointer. piglatin[count] is a
char. Use ' '.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top