Help!! How to return a pointer to beginning of string??

This is a discussion on Help!! How to return a pointer to beginning of string?? within the C++ Programming forums, part of the General Programming Boards category; I'm sorry- but this is for an assignment and I am stuck.. I am trying to convert a string into ...

  1. #1
    lms
    lms is offline
    Registered User
    Join Date
    May 2002
    Posts
    6

    Question Help!! How to return a pointer to beginning of string??

    I'm sorry- but this is for an assignment and I am stuck.. I am trying to convert a string into uppercase..and then return a pointer to the beginning of the string in main..and then somehow copy the pointer into a string again.. when I try to compile I get the following error message..
    error C2440: '=' : cannot convert from 'char *' to 'char [35]'
    There are no conversions to array types, although there are conversions to references or pointers to arrays

    I'm not going to post the entire program because it's lengthy...and am hoping that somene could point me in the right direction from the pieces below..

    Any suggestions or help would be greatly appreciated!!

    lms






    Code:
    //function prototype
    char* toUpperString(char[]);
    
    //function call from main
    char nameString[35];
    
    cout<<"Enter name: ";
    cin.getline(nameString,35);
    nameString=toUpperString(nameString);
    strncpy(e->name, nameString, 35);
    
    //function to convert to uppercase..
    
    char* toUpperString(char x[])
    {
    	int length;
    	char* string;
    
    	for (int i=0; i<length; i++)
    	{
    	string[i]=toupper(x[i]);
    	}
    	
    	return string;
    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Here is a function that will modify the string you pass to it.
    Code:
    void StringToUpper(char * s)
    {
      int length = strlen(s);
      for( int i=0 ; i<length ; i++)
      {
          s[i] = toupper( s[i] );
       }
    }
    //use like so.
    char string1[6] = "hello";
    StringToUpper(string1);
    If you want to save the original string, make a copy of it, and call the function on the copy

    Here is a function that will create a new string that you must later free using delete
    Code:
    char * StringToUpper(const char * s)
    {
       int length = strlen(s);
       char * ret = new char[length];
       for( int i=0 ; i<length ; i++)
       {
          ret[i] = toupper(s[i]);
       }
      return ret;
    }
    //use like so
    char string1[6] = "hello";
    char * string2 = StringToUpper(string1);
    //before the end of main do this
    delete [] string2;
    This version does NOT modify the original string, but you must delete the memory, before the var goes out of scope. I think this is a little messy, the first version is better.

  3. #3
    Unregistered
    Guest

    Re: Help!! How to return a pointer to beginning of string??

    Originally posted by lms
    Code:
    //function prototype
    char* toUpperString(char[]);
    
    //function call from main
    char nameString[35];
    
    cout<<"Enter name: ";
    cin.getline(nameString,35);
    nameString=toUpperString(nameString);
    strncpy(e->name, nameString, 35);
    
    //function to convert to uppercase..
    
    char* toUpperString(char x[])
    {
    	int length;
    	char* string;
    
    	for (int i=0; i<length; i++)
    	{
    	string[i]=toupper(x[i]);
    	}
    	
    	return string;
    }
    well, you can do this:
    Code:
    >>char* toUpperString(char[]);
    char toUpperString(char*x);
    
    -edited version of toUpperString:
    char toUpperString(char *x)
    {
    int length;
    char* string;
    for (int i=0; i<length; i++)
    {
    string[i]=toupper(x[i]);
    }
    return string;
    }
    
    question: do you want to make a copy of the string?
    if not, you can save memory by removing that extra pointer.
    Regards,
    toaster

  4. #4
    lms
    lms is offline
    Registered User
    Join Date
    May 2002
    Posts
    6
    I'm still kind of confused..I'll keep trying to figure it out..

  5. #5
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cstring>
    #include <cctype>
    
    //function prototype
    void toUpperString(char[]); //void toUpperString(char*) are equivalent
    
    int main() {
        char nameString[35];
        cout<<"Enter name: ";
        cin.getline(nameString,34);//Should be 34 not 35
        toUpperString(nameString);//Using a pointer
        cout << nameString<<endl;
    	return 0;
    }
    
    void toUpperString(char x[]) {
        int length=strlen(x);
        //char* string;
    
    	for (int i=0; i<length; i++) {
            x[i] = toupper(x[i]);
        }
        return;
    }
    [EDIT]
    BTW I've changed it to "void" because when using a pointer (which is what the name of a char array is, a pointer to the 0th element), you a directly accessing the original variable. So you are not make a copy, this saves on overhead. Which isn't really important for strings of this size, but it does matter when copy structures as such. But the principle is the same. Also you didn't tell the for loop what the length of the string was. And you were also return a pointer to something that didn't exist. The variable "char *string", nor did you copy the existing contents of the string "x" into the variable "string". This would have caused a segmentation fault. A seg fault is accessing memory that it doesn't have permission to access or does not exist. Hope this helps
    [/EDIT]
    kwigibo
    Last edited by kwigibo; 06-03-2002 at 04:22 AM.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    You weren't so far off, Ims. But just remember: When you declare a pointer, *do not* try to access it till it has either been allocated for with malloc, calloc, etc or else has been pointed to a valid object.

    Also, you did not calculate the value of "length".


    Code:
    
    char* toUpperString(char x[])
    {
    	int length = strlen(x);
    	char* string;
    
    	for (int i=0; i<length; i++)
    	{
    	x[i]=toupper(x[i]);
    	}
    	
                    string = x;
    
    	return string;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21