I'm very very new to C so I'm having some trouble with this (and pointers):
I'm trying to pass four parameters into a function:

birthdayFunction(char birthday, int month, int day, int year);

The birthday is the string input from the user (with "/" separating each field, ex. 7/23/76) and I want to separate each field into the corresponding month, day, year variable, just so they are represented as integers. I don't want this function to have any return values, just void.

I'm thinking I have to pass the parameters' memory location (&) in the main function, and then in the birthdayFunction I have to use the pointers?

I'm sorry if that's a bit confusing!

Thanks for any help!

First parameter is wrong. Use char* instead of char. Are you going to change the value of those variables inside the function? If so, pass pointers or reference, else you can simply pass by values.

If you are looking to convert strings and into integers, you may want to check out the atoi() function.

Chris

Consider also sscanf for parsing the source string.

#include <stdio.h>

int main() 
{
   const char text[] = "7/23/76";
   int month, day, year;
   if ( sscanf(text, "%d/%d/%d", &month, &day, &year) == 3 )
   {
      printf("month = %d, day = %d, year = %d\n", month, day, year);
   }
   return 0;
}

/* my output
month = 7, day = 23, year = 76
*/
commented: I vote for that option. +10

If you are looking to convert strings and into integers, you may want to check out the atoi() function.
Chris

Not my first or second choice. atoi() falls short at error handling.
strtol() is a better alternative.

Not my first or second choice. atoi() falls short at error handling.
strtol() is a better alternative.

That is too true, i'm glad you pointed that out. strtol() may prove helpful to me thanks.

Chris

Strings are passed by reference by default...
atoi-> Like salem said error handling is bit tough...
sscanf-> is what i would have used, it would separate as well as convert it to int format(or what ever format)...
strtol->Parses the C string str interpreting its content as an integral number of the specified base, which is returned as a long int value
long int strtol ( const char * str, char ** endptr, int base );

Dave, I used the sscanf you suggested and it works exactly how I wanted it to. Thank you!

Thanks to everyone else for explaining everything, especially the atoi() function and the benefits of strtol()!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.