String class

Hi can any one tell me how to pass a string as an argument in a funtion or a method of a class,and its further use in that method or function for certain operation over it... like below

#include
#include
#include
char myname(char m);
int main()
{
char m[40]="vicky one";
  myname();
}
char myname(char m)
{ char abc[80];
abc=m||m;
return abc;
}

its is only a sample code(according to my mind), i wish u understand what i want to know for string as an argument and its further use in that method or function. ThanXXX..........

Replies

  • Nikhil Lokhande
    Nikhil Lokhande
    Pass base address of string...

    myname(&m);

    retrieve as

    char myname (char *m)
  • rahul69
    rahul69
    Vicky One
    Hi can any one tell me how to pass a string as an argument in a funtion or a method of a class,and its further use in that method or function for certain operation over it... like below

    #include
    #include
    #include
    char myname(char m);
    int main()
    {
    char m[40]="vicky one";
      myname();
    }
    char myname(char m)
    { char abc[80];
    abc=m||m;
    return abc;
    }

    its is only a sample code(according to my mind), i wish u understand what i want to know for string as an argument and its further use in that method or function. ThanXXX..........
    Since this string is basically an array of characters, u can pass it as an array is passed (ie pass by reference) or u can send an array into a character pointer which will hold the address of first element of array.
    See this example for better understanding:
    Using Arrays:
    #include
    #include
    
    void myname(char name[])
    {
    cout<<"My name is "<Using Pointers:
    #include
    #include
    
    void myname(char *pname)
    {
    cout<<"My name is "<
                                        
  • Vicky One
    Vicky One
    Thx alot ,,,i got some idea .. @#-Link-Snipped-#. Your way of telling the solution us owsome...
    but what about character pointer if it holds the address of 1st element,what if our array has more than one element..?

    Will all of the index be accessed???
  • rahul69
    rahul69
    Vicky One
    Thx alot ,,,i got some idea .. @#-Link-Snipped-#. Your way of telling the solution us owsome...
    but what about character pointer if it holds the address of 1st element,what if our array has more than one element..?

    Will all of the index be accessed???
    Arrays are stored in continuous memory locations. So when we increment the pointer, it will give the address of next memory location,
    So for example, if we have an integer array referenced with the pointer, say P,
    then P+1 will represent the address of next element, and it's value can be accessed as *(P+1) or P[1]. 😀 Note: Here we are not changing value of the pointer

    Although, if u will move your pointer as P=P+1 (or P++), then u will lose your first element, but generally u don't need to do that, on the bright side u can use same pointer for different arrays whenever needed, by changing the address it points to.

You are reading an archived discussion.

Related Posts

compiler,assembler,word processor,linker AMONG THIS WHICH IS NOT A SYSTEM SOFTWARE?? i am confused..
Microsoft has made official the release of Windows 8.1 Update. Now, everyone owning a Windows 8 device can download Windows 8.1 for its new features that aim at addressing the...
Samsung Group India has launched two new Android smartphones in the market with Samsung Galaxy Star Pro and Samsung Galaxy Trend. Bringing forth these new mid-segment smartphones, Samsung India has...
Everyone's favorite childhood game is now on browser. https://www.fullscreenmario.com/ Games look good and graphics are also very good. You can select any level to play. Also you can generate your...
Kobo, the eReader manufacturing company, has launched a bunch of its products in India. All eReaders, after tying up with leading bookstores in India such as Crossword and WHSmith as...