I have no problems connecting strings but if I follow the same procedure for chars then the program blows.

void  Input_Code(char *d1_p, char *d2_p, char *d3_p, char *d4_p, char *d5_p,
		 char *d6_p, char *d7_p, char *d8_p, char *d9_p, char *d10_p, char *full_code_p)
{
   char buffer[12];
   printf("Please enter 10 character account code: ");
   fgets(buffer, sizeof buffer, stdin);
   sscanf(buffer, "%c%c%c%c%c%c%c%c%c%c",
      d1_p,d2_p,d3_p,d4_p,d5_p,d6_p,d7_p,d8_p,d9_p,d10_p);

   strcpy(full_code_p, d1_p);
   strcat(full_code_p, d2_p);
   strcat(full_code_p, d3_p);
   strcat(full_code_p, d4_p);
   strcat(full_code_p, d5_p);
   strcat(full_code_p, d6_p);
   strcat(full_code_p, d7_p);
   strcat(full_code_p, d8_p);
   strcat(full_code_p, d9_p);
   strcat(full_code_p, d10_p);
}

I just need to connect all 10 characters into one string.

Recommended Answers

All 12 Replies

What are you doing? Why do you need to have all these objects as function parameters when you input them in that same function, and where are all these objects defined? If all these "characters" are indeed defined as character arrays with the length of at least two characters, and the second character in all of these is '\0', well and everything else is correct, then your program could even work, otherwise it of course crashes. And of course you also have to consider, what if your dear user didn't enter ten characters, but less, then there would be '\n' and '\0' in your string, followed by some garbage characters, which you likely don't need in your string. But there is no need to add characters with strcpy, you can simply assign a character to a member of the character array, like buffer = c, where c is char c. But then of course you must always remember that the last character in a string must be '\0', which is the only way how it is possible to find the end of the string. What is good though, is that you input with fgets, not directly with scanf.

you got me confused a bit. I have to do it that way because I need to have the code as a string and in 10chars because I have to process them also one by one later in the program. I'm using parameters because what I posted is just a function definition. I call it from main. All I need to know is how to connect those char into one string using one of the functions from string.h.

Ohh i would say you get me a bit confused, with a strange code... But anyway, you don't need no functions from string.h to add characters to a string, but if you really like to do it with a function, then the only reasonable option is

sprintf (buffer, "%s%c", buffer, char);

but this function is in stdio.h, not string.h

with sprintf i got some more luck, but when I print it later on, there is some junk that is printed after it

does this looks ok?

sprintf(full_code_p, "%s%c%c%c%c%c%c%c%c%c%c", buffer, d1_p,d2_p,d3_p,d4_p,d5_p,d6_p,d7_p,d8_p,d9_p,d10_p);

with sprintf i got some more luck, but when I print it later on, there is some junk that is printed after it

If printf is displaying garbage is because you are missing the '\0' ( null
terminator ) at the end of the string. You need to add it.

Do you know the difference between 'x' and "x"?

Well, it's OK, in case of

char d1_p, d2_p, d3_p, d4_p,
    d5_p, d6_p, d7_p, d8_p, d9_p, d10_p;
sprintf (full_code_p,
    "%s%c%c%c%c%c%c%c%c%c%c",
    full_code_p,
    d1_p, d2_p, d3_p, d4_p,
    d5_p, d6_p, d7_p, d8_p, 
    d9_p, d10_p);

But your d1_p etc were not characters but pointers to character, as they appeared in your sscanf without &...

One more thing... If you write characters into string with sprintf, you don't need to add '\0' in the end of string, because sprintf does it.

If printf is displaying garbage is because you are missing the '\0' ( null
terminator ) at the end of the string. You need to add it.

Do you know the difference between 'x' and "x"?

thanks, and yes I do know the difference:)

yes I noticed no difference with using /0. Also after adding &to each digit, i still get a little junk in addition to the code.

ok, what's weird is that even if I don't input any code, i still get a little of this junk when I print full_code.

I still don't know why it was printing this junk but I eliminated the problem by %.10s printing and now it discards all the trash.

Thanks for the help

Watch the '\n' in the end of your string, this appears among your ten characters, if you entered less than ten characters, well, and the rest of your characters would be garbage in that case, if you didn't define them as global, or init ether to space or zero...

Ohh yes, make sure to do this

char full_code_p [FILENAME_MAX]; /*>=11*/
full_code_p [0] = '\0';
/* or */
strcpy (full_code_p, "");

anywhere *before* you write your characters with sprintf.

I have no problems connecting strings but if I follow the same procedure for chars then the program blows.

void  Input_Code(char *d1_p, char *d2_p, char *d3_p, char *d4_p, char *d5_p,
         char *d6_p, char *d7_p, char *d8_p, char *d9_p, char *d10_p, char *full_code_p)
{
   char buffer[12];
   printf("Please enter 10 character account code: ");
   fgets(buffer, sizeof buffer, stdin);
   sscanf(buffer, "%c%c%c%c%c%c%c%c%c%c",
      d1_p,d2_p,d3_p,d4_p,d5_p,d6_p,d7_p,d8_p,d9_p,d10_p);

   strcpy(full_code_p, d1_p);
   strcat(full_code_p, d2_p);
   strcat(full_code_p, d3_p);
   strcat(full_code_p, d4_p);
   strcat(full_code_p, d5_p);
   strcat(full_code_p, d6_p);
   strcat(full_code_p, d7_p);
   strcat(full_code_p, d8_p);
   strcat(full_code_p, d9_p);
   strcat(full_code_p, d10_p);
}

I just need to connect all 10 characters into one string.

I hope you know this function is only worth for educational purposes in how to handle chars and pointers. Said that here's what you missing.
These are the prototypes for strcpy and strcat. Both of them takes two strings parameters and returns a string each.
char *strcpy( char *to, const char *from );
char *strcat( char *str1, const char *str2 );

You are passing the wrong parameters to these functions. You are passing a character and not a string.
Does why I asked you if you new the difference between 'x' and "x".
sustitude all those strcats and the strcpy for:
full_code_p[0] = *d1_p; and so on. At the end you need to add the string terminator.
full_code_p[10] = '\0';

Or you would have to add a '\0' to all those char *d1_p, etc, after you pick a character in each after the sscanf. Something like this:

#include <stdio.h>



void  Input_Code(char *d1_p, char *d2_p, char *d3_p, char *d4_p, char *d5_p,
         char *d6_p, char *d7_p, char *d8_p, char *d9_p, char *d10_p, char *full_code_p)
{
   char buffer[12];
   printf("Please enter 10 character account code: ");
   fgets(buffer, sizeof buffer, stdin);
   sscanf(buffer, "%c%c%c%c%c%c%c%c%c%c",
      d1_p,d2_p,d3_p,d4_p,d5_p,d6_p,d7_p,d8_p,d9_p,d10_p);
   
   *( d1_p + 1 ) = '\0';
   *( d2_p + 1 ) = '\0';
   *( d3_p + 1 ) = '\0';
   *( d4_p + 1 ) = '\0';
   *( d5_p + 1 ) = '\0';
   *( d6_p + 1 ) = '\0';
   *( d7_p + 1 ) = '\0';
   *( d8_p + 1 ) = '\0';
   *( d9_p + 1 ) = '\0';
   *( d10_p + 1 ) = '\0';

   strcpy(full_code_p, d1_p);
   strcat(full_code_p, d2_p);
   strcat(full_code_p, d3_p);
   strcat(full_code_p, d4_p);
   strcat(full_code_p, d5_p);
   strcat(full_code_p, d6_p);
   strcat(full_code_p, d7_p);
   strcat(full_code_p, d8_p);
   strcat(full_code_p, d9_p);
   strcat(full_code_p, d10_p);
}



int main( void )
{
    char ch1_p[2], ch2_p[2], ch3_p[2], ch4_p[2], ch5_p[2], ch6_p[2], ch7_p[2], ch8_p[2], ch9_p[2], ch10_p[2], full_code_p[11];
    
    Input_Code( ch1_p,ch2_p,ch3_p, ch4_p, ch5_p, ch6_p, ch7_p, ch8_p, ch9_p, ch10_p,full_code_p );
    printf( "%s\n", full_code_p );
    
    while( getchar() != '\n' );
    return 0;
}
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.