![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 3
Rep Power: 0
![]() |
Returning An Array From a Function
Hey all,
I'm currently writing a calculator program for Uni which takes in 2 Roman Numeral expressions and displays the answer Roman Numerals. The answer is then displayed in Roman Numerals i.e IX+XV = XXIV The problem i'm having is trying to return an array from a function. The function takes in the integer answer worked out earlier in the program, and then converts this to roman numerals, saving each character in an array, which is created by the function. I wish to return the array back into the main program but i'm having problems Sorry if this has been asked before, I tried searching the forums but couldn't find anything that similar . The code for my sub-routine is as follows: char convert(int answer)
{
int j=0;
char numeral[20];
// Calculate M's
while((answer-1000)>=0)
{
answer=answer-1000;
numeral[j++]='M';
}
if(answer>=900)
{
numeral[j++]='C';
numeral[j++]='M';
answer=answer-900;
}
// Calculate D's
while((answer-500)>=0)
{
answer=answer-500;
numeral[j]='D';
j++;
}
if(answer>=400)
{
numeral[j++]='C';
numeral[j++]='D';
answer=answer-400;
}
// Calculate C's
while((answer-100)>=0)
{
answer=answer-100;
numeral[j++]='C';
}
if(answer>=90)
{
numeral[j++]='X';
numeral[j++]='C';
answer=answer-90;
}
//Calcalute L's
while((answer-50)>=0)
{
answer=answer-50;
numeral[j++]='L';
}
if(answer>=40)
{
numeral[j++]='X';
numeral[j++]='L';
answer=answer-40;
}
// Calculate X's
while((answer-10)>=0)
{
answer=answer-10;
numeral[j++]='X';
}
if (answer==9)
{
numeral[j++]='I';
numeral[j++]='X';
answer=answer-9;
}
// Calculate V's
while((answer-5)>=0)
{
answer=answer-5;
numeral[j++]='V';
}
if(answer==4)
{
numeral[j++]='I';
numeral[j++]='V';
answer=answer-4;
}
// Calculate j's
while((answer-1)>=0)
{
answer=answer-1;
numeral[j++]='I';
}
numeral[j] = '\0';
printf("\n");
return numeral;
}I want to return the array numeral so that it can be displayed on screen and the code can be used to convert other numbers. These are the errors that the compiler is displaying: Warn : roman.c(207,19) uspicious pointer conversionError: roman.c(207,19):Nonportable pointer conversion Can someone help me out so that i can get my program working properly? Thanks in advance, Dave ![]() |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
char *convert(int answer)
{
int j=0;
char numeral[20];
// Calculate M's
while((answer-1000)>=0)
{
answer=answer-1000;
numeral[j++]='M';
}
if(answer>=900)
{
numeral[j++]='C';
numeral[j++]='M';
answer=answer-900;
}
// Calculate D's
while((answer-500)>=0)
{
answer=answer-500;
numeral[j]='D';
j++;
}
if(answer>=400)
{
numeral[j++]='C';
numeral[j++]='D';
answer=answer-400;
}
// Calculate C's
while((answer-100)>=0)
{
answer=answer-100;
numeral[j++]='C';
}
if(answer>=90)
{
numeral[j++]='X';
numeral[j++]='C';
answer=answer-90;
}
//Calcalute L's
while((answer-50)>=0)
{
answer=answer-50;
numeral[j++]='L';
}
if(answer>=40)
{
numeral[j++]='X';
numeral[j++]='L';
answer=answer-40;
}
// Calculate X's
while((answer-10)>=0)
{
answer=answer-10;
numeral[j++]='X';
}
if (answer==9)
{
numeral[j++]='I';
numeral[j++]='X';
answer=answer-9;
}
// Calculate V's
while((answer-5)>=0)
{
answer=answer-5;
numeral[j++]='V';
}
if(answer==4)
{
numeral[j++]='I';
numeral[j++]='V';
answer=answer-4;
}
// Calculate j's
while((answer-1)>=0)
{
answer=answer-1;
numeral[j++]='I';
}
numeral[j] = '\0';
printf("\n");
return numeral;
}Example: char retFromFunc[20]; strcpy(retFromFunc, convert(3));
__________________
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2005
Posts: 3
Rep Power: 0
![]() |
I'm not completly sure where to add in youe example code
The code that calls the function at the moment is, answer2=convert(answer);
printf("\n");
printf("%s", answer2);and my function is declared at the top of my program as char convert(int) I'm new to C and pointers (only been learning C 4 weeks). Can you explain how to alter the other lines of my code so that the program works correctlly. Thanks in advance, Dave |
|
|
|
|
|
#4 |
|
Professional Programmer
|
you can't return an array in C you'll have to return a pointer to the array.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Feb 2005
Posts: 3
Rep Power: 0
![]() |
Yeah Dizzutch i've been told that already today but i'm not too sure how to do that
![]() I've only been learning C for 4 weeks Are you able to help me out? |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Dec 2004
Location: a cardboard box
Posts: 118
Rep Power: 4
![]() |
Edit: Actually Im not sure if this applies for C. Works for C++.
Umm Arrays automatically pass by reference (theres another term used for arrays, cant remember). Theres no need for a pointer.
__________________
... Last edited by Xero; Feb 21st, 2005 at 6:48 PM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|