Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,578 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,939 people online right now. Registration is fast and FREE... Join Now!




Convert string

 
Reply to this topicStart new topic

Convert string

Nickshawn
26 Sep, 2008 - 11:04 AM
Post #1

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 16


My Contributions
I have something like this:
CODE

int main(int argc, char *argv[]) {

char buffer[512];

        if(argc>2)
        {
                buffer = argv[2];  // HOW??
        }
        else
        {
                puts("Not enough parameters!");
                return 0;
        }

How can i convert argv[2] to char buffer?
Also, what are the technical names for these two?
Thank so much!
Nick
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Convert String
26 Sep, 2008 - 11:29 AM
Post #2

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,539



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
argv[2] is a pointer, so why not just use:
char *buffer = argv[2];

smile.gif
User is online!Profile CardPM
+Quote Post

Psionics
RE: Convert String
26 Sep, 2008 - 11:45 AM
Post #3

D.I.C Head
Group Icon

Joined: 6 Sep, 2008
Posts: 122



Thanked: 2 times
Dream Kudos: 100
My Contributions
to elaborate on what gabehabe said, pointers allow us to send values 'by-reference' instead of 'by-value'. The difference is when you use pointers, you don't have to make a copy of a variable, and instead you can just change the actual variable.

you can use a pointer just like this:
cpp

// declare a variable
int x;

// declare a pointer
int* ptr; // you can write this as int* ptr or int *ptr...same thing

// point the pointer to the address of x
ptr = &x // the & means the 'address of'

// now you can use the pointer to change and display x
*ptr = 10; // using the * 'dereferences' the pointer, showing the value

cout << "x = " << *ptr << endl; // will display 10


I just made this incase you didn't understand pointers smile.gif
User is offlineProfile CardPM
+Quote Post

Nickshawn
RE: Convert String
26 Sep, 2008 - 11:55 AM
Post #4

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 16


My Contributions
Nice! Thanks for the fast responce.
User is offlineProfile CardPM
+Quote Post

Psionics
RE: Convert String
26 Sep, 2008 - 11:57 AM
Post #5

D.I.C Head
Group Icon

Joined: 6 Sep, 2008
Posts: 122



Thanked: 2 times
Dream Kudos: 100
My Contributions
it's no problem, do you understand everything you need?
User is offlineProfile CardPM
+Quote Post

Nickshawn
RE: Convert String
26 Sep, 2008 - 12:07 PM
Post #6

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 16


My Contributions
I'll never understand everything i need...but i understand pointers well!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:20AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month