NO

Author Topic: Pointer as return-value of a function  (Read 2265 times)

scy

  • Guest
Pointer as return-value of a function
« on: November 04, 2015, 03:05:17 PM »
hey guys,

i am trying to write a function where i convert a string of numbers to their ASCII-equals

i know that a function can only have 1 return value which is why i want/need to have a pointer as a return value to return the whole string.

i know that i need a pointer but i have no clue how to write the function.

Code:
Code: [Select]
int *stoi(char str[]) {
int z;
int g = 0;

for (z = 0; str[z] != '\0'; z++) {
g = ctoi(str[z]);
str[z] = g;
}

return str;
}
this is the ctoi function:
Code: [Select]
int ctoi(char c) {
int i = 0;

i = c - '0';

return i;
}
this is the code i have for the function.
i want to call the function like this:
Code: [Select]
char *str = "-211";
int *x;
x = stoi(str);
printf("\n%i \n", *x);
return EXIT_SUCCESS;
and as you see i want to output the string into the console, but it just stays empty.

i am using eclipse-c++ on ubuntu linux

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Pointer as return-value of a function
« Reply #1 on: November 05, 2015, 12:32:37 AM »
i am using eclipse-c++ on ubuntu linux
Well, this forum is for Pelle's C, which only runs on Windows ;-)

And we usually don't do the homework for other either.

I am a bit short on time right now (just stopped by to check for spammers to zap), I will get back to you with some hints later if no one else is biting in the meantime ...   ;)

Ralf