473,480 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pointer conversion

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;
}
Nov 14 '05 #1
3 5599
fb wrote:
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

--
Artie Gold -- Austin, Texas

20050120->44
Nov 14 '05 #2
fb wrote:
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 */
Nov 14 '05 #3
fb wrote:

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 ' '.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2336
by: chessc4c6 | last post by:
The program below creates a char pointer call charPtr...... i then declare an char array string "Good Luck" When i assign charPtr = string, I expect an error. However, It actually runs and...
11
2083
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
16
2260
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
10
353
by: junky_fellow | last post by:
K&R say that, It is guaranteed that 1) a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and 2) back again without...
204
12881
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
28
2362
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: ...
48
2112
by: yezi | last post by:
Hi, all: I want to record some memory pointer returned from malloc, is possible the code like below? int memo_index; int i,j; char *tmp; for (i=0;i<10;i++){
49
2730
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
6
2872
by: Lighter | last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to- pointer standard conversionsare not applied to the left expressions"? In 5.18 Comma operator of the C++ standard, there is a...
8
1833
by: tfelb | last post by:
Hey group! I have 2 questions. I saw functions with char *dst = (char *)src. In that case if I remember what I've learned I assign (an) (the) address of src to dst. Right? But I can assign...
0
7051
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7097
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6750
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.