CrazyEngineers
  • Vicky One
    Vicky One

    MemberOct 16, 2013

    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
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Nikhil Lokhande

    MemberOct 16, 2013

    Pass base address of string...

    myname(&m);

    retrieve as

    char myname (char *m)
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberOct 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");
    }
    
    Are you sure? This action cannot be undone.
    Cancel
  • Vicky One

    MemberOct 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???
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberOct 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.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register