开发者

C String input confusion

开发者 https://www.devze.com 2022-12-26 02:01 出处:网络
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:

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


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;
}


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);  
}


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);

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消