String class

Vicky One

Vicky One

@vicky-one-9xiLwc Oct 27, 2024
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<iostream.h>
#include<conio.h>
#include<string.h>
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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Nikhil Lokhande

    Nikhil Lokhande

    @nikhil-lokhande-EXmWKM Oct 16, 2013

    Pass base address of string...

    myname(&m);

    retrieve as

    char myname (char *m)
  • rahul69

    rahul69

    @rahul69-97fAOs Oct 16, 2013

    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<iostream.h>
    #include<conio.h>
    #include<string.h>
    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<iostream.h>
    #include<stdlib.h>
    
    void myname(char name[])
    {
    cout<<"My name is "<<name;   
    }
    int main()
    {
    char name[50]="Vicky one";
    myname(name); 
    system("Pause");
    }
    
    Using Pointers:
    #include<iostream.h>
    #include<stdlib.h>
    
    void myname(char *pname)
    {
    cout<<"My name is "<<pname;   
    }
    int main()
    {
    char name[50]="Vicky one";
    myname(name); 
    system("Pause");
    }
    
  • Vicky One

    Vicky One

    @vicky-one-9xiLwc Oct 16, 2013

    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

    @rahul69-97fAOs Oct 19, 2013

    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.