Page 2 of 2
Re:Completely stumped
Posted: Wed Jul 30, 2003 4:59 am
by mystran
The reverse subscripting thing, AFAIK has something to do with [] being normal binary operator, with a somewhat unregular syntax.
Basicly a[b+2] is interpreted as *(a + (b + 2)) if a is a pointer.. since adding n to a pointer of type "foo *" actually adds n*sizeof(foo) to the address.
Now, because of that adding a pointer and a value together doesn't depend on the order they are written in, b[a+2] just evaluates to *(b + (a+2)) which is exactly the same thing.
So generalizing, in old C, a
is exactly the same as *(a+(b)) where a and b can be arbitary expressions, as long as you can add them together, and dereference the result.
Another thing is that for array such as "char * foo[123]" both "foo" and "&foo" are the same thing as "&foo[ 0 ]" which does or does not make sense, depending on how you look at it.
If there are errors, someone please correct me
Re:Completely stumped
Posted: Wed Jul 30, 2003 6:25 am
by Kon-Tiki
First of all: I know shiznit of VB. Tried that some years ago, but without at help-file, there's no starting.
Now, this's my code after the changes I made when reading Joel's post:
Code: Select all
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char condition(char* seat, char* pos, char* object) {
printf("Toilet seat is %s and you are %s. There is %s on the seat. \n", seat, pos, object);
}
char input(char* seat, char* pos, char* object) {
scanf("Raise or lower the seat? \n", &seat);
fflush(stdin);
scanf("Stand or sit? \n", &pos);
fflush(stdin);
scanf("Poo or pee? \n", &object);
fflush(stdin);
}
int main()
{
char seat[10];
char pos[10];
char object [10];
input(seat, pos, object);
condition(seat, pos, object);
system("pause");
return 0;
}
This's what I put in:
The output:
Code: Select all
Druk op een toets om door te gaan. . .
Toilet seat is ?S1< and you are ??S??????SU??S1<. There is ??S-@ on the seat.
I'll go and read that book I showed Tim and put those &-s in the code.
-Kon-Tiki-
Re:Completely stumped
Posted: Wed Jul 30, 2003 6:52 am
by Joel (not logged in)
You need to read up on scanf. You're not using it correctly. scanf doesn't print anything, IIRC. If you want to prompt the user you have to use printf followed by a scanf.
As in:
printf("Stand or sit?\n");
scanf("%s", pos);
Also notice the lack of the address-of operator in front of [tt]pos[/tt]. [tt]pos[/tt] is a pointer which means that it stores the address of your character array. [tt]&pos[/tt] is the memory address where the pointer itself is stored.
Schol-R-LEA: the biggest difference between arrays and pointers is in the sizeof operator.
char seat[10];
char* pseat = seat;
In this code, sizeof(seat) != sizeof(pseat). These values are the size of the array (10 bytes) and the size of a pointer (4 bytes on 32-bit systems), respectively.
Re:Completely stumped
Posted: Wed Jul 30, 2003 7:32 am
by Kon-Tiki
I understand what I did wrong and fixed it. It works now.
Seeing what Joel said, I understand that C++ interpreted the thing I put in the Scanf as a thing like %s, but since it was not, it just turned every character of it to an ascii code.
It works now. All I have to do now, is make it loop until the user enters 'Quit'. That's easily done with a switch-command.
As for the book: The one I have is around 380 pages. From the moment that this printer works, I'll start reading part by part (starting with the first 20 pages) That'll keep my newbie-questions to a minimum.
Oh, btw: anybody interested in this program when it's done (or my calculator, for that matter)? I'ld be glad to release those things.
-Kon-Tiki-