I am trying to write an function, which takes an string as input and do something and return the strings.
Here Is a small try from my side.


char algo(char n)
  char result[15];
  if(n=='1' || n=='2' || n=='3' || n=='4' || n=='5' || n=='6')
          result = n;
          result = '10';

        return result;


I am new to C.

6 Years
Discussion Span
Last Post by Adak

Not really sure what the question is...but you functions takes a character not a string.

I am new to C so my code can be wrong. But what I want is to write a function which takes a string as input, do some inside work and return the string.


Strings don't need to be returned from a function.

When you send a string to a function (which is what you want to do), you are sending a constant pointer (which is the name of the char array, itself).

And since the function has the address, rather than just the variable itself( like a number would be), then it can make changes into the char array, (the string).

#include <stdio.h>

void changeIt(char *mystring) {
   mystring[2] = 't';  

int main() {
  char mystring[10] = { "Ack!" };
  printf("\n%s", mystring);

  return 0;

So normally, you want to bring the array name into the function that will be working on it. Local arrays from the function itself, will terminate after the function executes - so trying to use it afterward is an error - sometimes you get away with it, but static local arrays, can't be trusted to work right, after their function is done.

Edited by Adak: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.