473,587 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ 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
"nonportabl e 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(cha r 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(engl ish, piglatin);
readinput(engli sh);

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

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

/* Convert english into piglatin */
convert(words, english, piglatin);
writeoutput(pig latin);
}
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(cha r piglatin[])
{
int count = 0;

for(count = 0; count < 80; count++)
putchar(piglati n[count]);
printf("\n");
return;
}
Nov 14 '05 #1
3 5608
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
"nonportabl e 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(cha r 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(engl ish, piglatin);
readinput(engli sh);

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

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

/* Convert english into piglatin */
convert(words, english, piglatin);
writeoutput(pig latin);
}
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(cha r piglatin[])
{
int count = 0;

for(count = 0; count < 80; count++)
putchar(piglati n[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
"nonportabl e 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$g E.280449@pd7tw3 no>). 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 "nonportabl e 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********@yah oo.com) (cb********@wor ldnet.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
2348
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 outputs: G Good Luck
11
2105
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
2285
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 subject pointer does not refer to an object suitably aligned in storage. It is guaranteed that a pointer to an object may be converted to a pointer to an...
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 change. My question is that, is it legal to dereference the intermediate pointer (the pointer with less strict alignment that we get after...
204
12970
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 = {0,1,2,4,9};
28
2383
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: int main() {
48
2144
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
2769
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 ;-): char *cp; void *vp; void **vpp;
6
2894
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 rule: "The lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard conversionsare not applied to the left expressions" I could...
8
1849
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 an address with the address operator & too? char *dst = &src. What's the difference between *dst = (char *)src and *dst = &src and
0
7915
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6619
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
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 we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.