1

C really isn't my strong point and after reading 3 chapters of a book on the subject and spending ages trying to get stuff working it just doesn't:

#include <stdio.h>
char *a,*b;
int main( )
{
     char input[10];
     fgets(input,sizeof input, stdin);
     a = input;
     fgets(input,sizeof input, stdin);
     b = input;
     printf("%s : %s",a,b);

}

I've isolated the problem from my main project. This code is meant to read in two strings and then print them however it seems to be setting a and b to point to input. Sample output from this code when A and B are entered is(don't worry about the \n's i can remove them):

A
B
B
 : B

How do i store the value of input in another variable eg. a or b so that in the above case

A
B
A
 : B

Is output?

NOTE: I don't want a or b to point to another variable i want to store a string in them:

a = "A";
b = "B";

would be the string literal equivalent.

Thanks

4 Answers 4

4

You'll have to either declare a and b as separate arrays as large as your input array, or dynamically allocate memory to them. Either way, you'll have to use strcpy() to copy the contents of one string to another:

#include <stdio.h>
#include <string.h>

int main(void)
{
  char input[10], a[10], b[10];
  fgets(input, sizeof input, stdin);
  strcpy(a, input);
  fgets(input, sizeof input, stdin);
  strcpy(b, input);
  printf("%s : %s\n", a, b);
  return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

2

a is a pointer to input, as is b. Setting a = input just sets a to point to input.

So your calls to fgets affect the same buffer.

You should use two separate arrays for A and B, and copy the data from input into A and B.

NOTE: I don't want a or b to point to another variable i want to store a string in them:

a = "A";

b = "B";

would be the string literal equivalent.

Unfortunately, C doesn't quite work the way you think it does. You're too used to higher-level lanaguages who do the copying for you.

In C, if you want A and B to contain data, you have to specify the data container, and put the data in there.

For example:

int main( ) 
{ 
     char input[10]; 
     char aData[10];
     char bData[10];     
     fgets(input,sizeof input, stdin); 
     strcpy(aData, input);
     fgets(input,sizeof input, stdin); 
     strcpy(bData, input);
     printf("%s : %s",aData,bData);  
}

1 Comment

Thanks, Yeah higher level languages are too helpful sometimes :D
1

You need to use separate arrays for each input, so the second input doesn't overwrite the first.

#include <stdio.h>
int main( )
{
     char a[10];
     char b[10];
     fgets(a,sizeof a, stdin);
     fgets(b,sizeof b, stdin);
     printf("%s : %s",a,b);

}

3 Comments

There isn't any difference in C. Strings are represented by pointers to sequences of characters (terminated by a zero byte). You could always copy the string into another buffer (e.g., one allocated dynamically) before reusing input for the second read, but that takes more code...
That would work for the example but not for the actual project. In the project i want to store input and then later set a to it.
You can't set a to hold more than one char[], but you're welcome to try.
1

Use 2 different char buffer arrays.

A pointer is just a varaible that holds a memory address.

So in the following code:

a = input;

and

b = input;

Both a and b hold the same memory address, that memory address is the address of the first element of the array.

This is the correct code:

char input1[10];
char input2[10];
fgets(input,sizeof input1, stdin);
fgets(input,sizeof input2, stdin);
printf("%s : %s",input1, input2);

Also when you take sizeof(pointer) you will get 4 on an x86 system always. You don't get the size of the element you are pointing to.

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.