1

In C++ if you want to input a string and store it in a char array from index 1 you can do something like this:

ifstream f("data.in");
char a[100];
f.get(a+1,90);

Is there any way you can do this in C (like using fscanf)? Doing this:

fscanf(f,"%s",&a+1);

or this:

fscanf(f,"%s",&(a+1));

doesn't work.

4
  • a already is a pointer, you don't have to use & Commented Jan 5, 2015 at 22:10
  • a+1 should work as well Commented Jan 5, 2015 at 22:11
  • use fscanf(f, "%s", a+1); Commented Jan 5, 2015 at 22:11
  • &a + 1 generates an address 100 bytes beyond the start of a, because &a is char (*)[100] in this context, and incrementing a pointer by one jumps it by the size of the object being pointed at (which is an array of 100 bytes in this case). A good compiler would also complain about the type mismatch between expected char * and actual char (*)[100]. Commented Jan 5, 2015 at 22:12

1 Answer 1

2

Yes there is, this way

fscanf(f,"%90s",a+1);
Sign up to request clarification or add additional context in comments.

2 Comments

It's to limit the number of characters fscanf must read, I suppose that's what f.get(a+1,90) does.
Also &a[1] for the third argument.

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.