×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

returning a *string and string[] from a function

returning a *string and string[] from a function

returning a *string and string[] from a function

(OP)
Hello,

This might be better explained with my code below. if you are return a *string or a string[].

The idea is to convert the whole string to upper case without depending on any other libraries.

However, if the user passes in a *string or a string[] would it depend on how I return them to the calling function. This is what I am experimenting with here.

I thought if I new what was being used, I could return based on that.

Any corrections that are needed for my code, please advise.

Many thanks,


CODE

char* ToUpper(char *str, const int length)
{
    char *reset = str; //For reseting the pointer to point to the ordinal address.
    int character;
    char *destString = malloc(length * sizeof(char));
    int i = 0;

    //Check to see if actual memory was allocated.
    if(destString == NULL)
    {
        return NULL;
    }

    //Copy the contents to the array.
    //A pointer to an array cannot be modified
    while(*str)
    {
        destString[i] = *str++;
        i++;
    }
    destString[i] = '\0'; //Terminate the end of the string.

    str = reset; //Reset the pointer to point to the first character in the character array.

    i = 0;
    while(*str)
    {
        //Get the character and subtract 32 to make it upper case.
        //Filter for only lower case characters.
        if(*str >= 97 && *str <= 122)
        {    
            //Get the character that is lower case and subtrack 32 to make it a upper case character
            character = (int) destString[i];
            character -= 32;
            //Assign the upper case character back into its position in the array.
            destString[i] = (char) character;
        }    
        str++;    
        i++;
    }

    destString[i] = '\0';
    //Point the character array about to the first character in the array.
    //str = reset;
    str = destString;

    return destString;
}


int main(int argc, char** argv)
{
    char *string1 = "Hello how are you today?";
    char string2[] = "Hello how are you today?";

    string1 = ToUpper(string1, strlen(string1));
    printf("string1: %s\n", string1);
    string2[0] = ToUpper(string2, strlen(string2));
    printf("string2: %s\n", string2);

    return 0;
}

RE: returning a *string and string[] from a function

You need to malloc length+1.  strlen returns the number of characters excluding the terminator.

 

RE: returning a *string and string[] from a function

Since ToUpper() returns a pointer to newly allocated memory, the caller needs to remember to free() that memory.

CODE

//Get the character and subtract 32 to make it upper case.
//Filter for only lower case characters.
if(*str >= 97 && *str <= 122)
{
    //Get the character that is lower case and subtrack 32 to make it a upper case character
    character = (int) destString[i];
    character -= 32;
    //Assign the upper case character back into its position in the array.
    destString[i] = (char) character;
}
This is NOT portable.
Use the toupper() function if you want portable code.

RE: returning a *string and string[] from a function

The codes may be reduced. you don't need destString and you should get length as a stopper of while loop. Be carefully coding in C, don't let memory leak.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close