1
milage = atoi(strtok(NULL, " "));
drive(&cars[carID-1], milage);

I have something like this for numbers, I want to use same thing for a name (character).

I tried this:

user = strtok(NULL, " ");
rent(&cars[carID-1], user);

but it did not work out.

Can any one help?

1
  • Define work first. Give minimal example of the code that can be compiled and ran, and describe what you want it to do. Otherwise this is not a question. Commented Feb 16, 2011 at 2:16

2 Answers 2

3

Are you just trying to extract numbers / strings from within another string? If that is the case you should probably have a look at sscanf. It works just like scanf but reads froms a string instead of from the standard input.

char name[100]; int mileage;
sscanf("username 42", "%s %d", name, &mileage);
//name now contains "username" and mileage now contains 42
Sign up to request clarification or add additional context in comments.

2 Comments

how should I use sscanf if there is incoming value from user?
I don't understand this question. Can't you just use the regular scanf in this case?
0

char * is not a string type. char * is a pointer to byte array. Byte array is the string type.

Strings can be copied with strdup(). Copied strings need to be freed with free().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.