Help with pointers, and char -> int
Author |
Message |
Joakim Persson (zala.. #1 / 13
|
 Help with pointers, and char -> int
A few questions for you experienced folks... As most newbies (I suppose), I'm having quite some trouble with pointers... #include <stdio.h> struct datatest { int digit; char name[50]; Quote: };
int main(void) { datatest test1, test2, *ptr; test1.digit=10, test1.name="Test object 1", test2.digit=10, test2.name="Test object 2"; ptr=&test1; printf("The content of the current digit is %d and the content of the current name is %c",*ptr.digit, *ptr.name); ptr++; printf("The content of the current digit is %d and the content of the current name is %c",*ptr.digit, *ptr.name); Quote: }
Error message: Structure required on left side of . or .* What's wrong with this code? I've understood the basic fundamentals of "int pointers", but what about structs? Does it have anything to do with the priorities of * and . ? Please enlighten me (and, as I think I've pretty adequately understood the basic fundamentals of C, tell me what to move on to...)... ... I also want some opinions about how to correct "clumsy code"... For example, this { ....... Quote: }
double pvnrt(double p, double V, double n, double T) { if (V==0) return (n*Rgas*T)/p; if (p==0) return (n*Rgas*T)/V; if (n==0) return (p*V)/(Rgas*T); if (T==0) return (p*V)/(Rgas*n); return -1; Quote: }
is a simple math/chemistry function... How can this be minimized? Also, if "x" is input, how do you process this? The function above works when you feed it with pvnrt(1,1,1,0), then returning T... how do you modify it so you can feed it with the char 'x' and return the unknown value (or return -1 if more than one 'x' is input)? Also, is this optimized? int dice(int n, int s) { int a=1, returnvalue=rand()%s; while (a++<=n) returnvalue+=rand()%s; return returnvalue; Quote: }
Notably, I want to keep my code as small as possible... Not just for optimization cases, but I also like the look of "clean code"... Thanks in advance... --Joakim Persson -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Libertarian, Power Metal Fanatic, Guitarist, Computer/Internet {*filter*} IRC: Efnet, Quakenet -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
Sun, 11 Mar 2001 03:00:00 GMT |
|
 |
Ben Pfaf #2 / 13
|
 Help with pointers, and char -> int
A few questions for you experienced folks... As most newbies (I suppose), I'm having quite some trouble with pointers... Your code and your error message reveal that in fact you're writing C++ code. As such, it would probably be more appropriate to post to a C++ newsgroup. -- (supporter of the campaign for grumpiness where grumpiness is due in c.l.c) Please: do not email me copies of your posts to comp.lang.c do not ask me C questions via email; post them instead
|
Sun, 11 Mar 2001 03:00:00 GMT |
|
 |
John Gord #3 / 13
|
 Help with pointers, and char -> int
Quote: >Error message: Structure required on left side of . or .*
when using a pointer-to-a-structure, you can't use . to access the members. you must use "->". --- John Gordon "No Silicon Heaven? Preposterous! Where would
|
Sun, 11 Mar 2001 03:00:00 GMT |
|
 |
Joakim Persson (zala.. #4 / 13
|
 Help with pointers, and char -> int
Quote:
>>Error message: Structure required on left side of . or .* >when using a pointer-to-a-structure, you can't use . to access >the members. you must use "->".
And, as I just found out, you can put the pointer in paranthesis... so my previous "*ptr.digit" should have been "(*ptr).digit". It works fine now. If anyone has any other comments, please respond about the "optimization" issues... BTW, I haven't understood the -> method... Please provide me with an example! Quote: >--- >John Gordon "No Silicon Heaven? Preposterous! Where would
|
Sun, 11 Mar 2001 03:00:00 GMT |
|
 |
Chris Oultra #5 / 13
|
 Help with pointers, and char -> int
Quote:
> A few questions for you experienced folks... > int main(void) > { > datatest test1, test2, *ptr; > printf("The content of the current digit is %d and the content of the > current name is %c",*ptr.digit, *ptr.name); > ptr++; > printf("The content of the current digit is %d and the content of the > current name is %c",*ptr.digit, *ptr.name); > }
another pointer is required here ie datatest test1, test2, *ptest1, *ptest2; then initialise pointers with ptest1 = &test1; ptest2 = &test2; ptr++ is now defunct, which was probably giving spurious results anyway and your printf statements will become printf("The content of the current digit is %d and the content of the current name is %c",*ptestn.digit, *ptestn.name);
|
Sun, 11 Mar 2001 03:00:00 GMT |
|
 |
John Gord #6 / 13
|
 Help with pointers, and char -> int
Quote: > BTW, I haven't understood the -> method... Please provide me with an > example!
just replace . with -> instead of "struct.member" use "struct->member" --- John Gordon "No Silicon Heaven? Preposterous! Where would
|
Sun, 11 Mar 2001 03:00:00 GMT |
|
 |
Ben Pfaf #7 / 13
|
 Help with pointers, and char -> int
And, as I just found out, you can put the pointer in paranthesis... so my previous "*ptr.digit" should have been "(*ptr).digit". It works fine now. If anyone has any other comments, please respond about the "optimization" issues... BTW, I haven't understood the -> method... Please provide me with an example! (*a).b is equivalent to a->b. -- (supporter of the campaign for grumpiness where grumpiness is due in c.l.c) Please: do not email me copies of your posts to comp.lang.c do not ask me C questions via email; post them instead
|
Sun, 11 Mar 2001 03:00:00 GMT |
|
 |
Joe Wrigh #8 / 13
|
 Help with pointers, and char -> int
Quote:
> A few questions for you experienced folks... > As most newbies (I suppose), I'm having quite some trouble with > pointers... > #include <stdio.h> > struct datatest { > int digit; > char name[50]; > };
You want to define a structur type. typedef struct s { int digit; char name[50]; Quote: } datatest;
Now in main() test1 and test2 become structures and ptr a pointer to structure. Quote: > int main(void) > { > datatest test1, test2, *ptr; > test1.digit=10, test1.name="Test object 1", test2.digit=10, > test2.name="Test object 2"; > ptr=&test1; > printf("The content of the current digit is %d and the content of the > current name is %c",*ptr.digit, *ptr.name); > ptr++; > printf("The content of the current digit is %d and the content of the > current name is %c",*ptr.digit, *ptr.name); > } > Error message: Structure required on left side of . or .* > What's wrong with this code? I've understood the basic fundamentals of > "int pointers", but what about structs? Does it have anything to do > with the priorities of * and . ? Please enlighten me (and, as I think > I've pretty adequately understood the basic fundamentals of C, tell me > what to move on to...)...
After assignment "ptr = &test1;" you must dereference the pointer to get the structure. "(*ptr).name" for example. C provides a short-hand for this construct as "ptr->name". They are otherwise equivalent. Quote: > ... > I also want some opinions about how to correct > "clumsy code"... For example, this > { > ....... > } > double pvnrt(double p, double V, double n, double T) > { > if (V==0) return (n*Rgas*T)/p; > if (p==0) return (n*Rgas*T)/V; > if (n==0) return (p*V)/(Rgas*T); > if (T==0) return (p*V)/(Rgas*n); > return -1; > } > is a simple math/chemistry function... How can this be minimized? > Also, if "x" is input, how do you process this? > The function above works when you feed it with pvnrt(1,1,1,0), then > returning T... how do you modify it so you can feed it with the char > 'x' and return the unknown value (or return -1 if more than one 'x' is > input)? > Also, is this optimized? > int dice(int n, int s) > { > int a=1, returnvalue=rand()%s; > while (a++<=n) returnvalue+=rand()%s; > return returnvalue; > } > Notably, I want to keep my code as small as possible... Not just for > optimization cases, but I also like the look of "clean code"... > Thanks in advance... > --Joakim Persson > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> Libertarian, Power Metal Fanatic, > Guitarist, Computer/Internet {*filter*} IRC: Efnet, Quakenet > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
--
"Everything should be made as simple as possible, but not simpler." --- Albert Einstein ---
|
Mon, 12 Mar 2001 03:00:00 GMT |
|
 |
Richard Stam #9 / 13
|
 Help with pointers, and char -> int
Quote: >BTW, I haven't understood the -> method... Please provide me with an >example!
Instead of (*ptr).digit write ptr->digit. The advantage may not be clear in this particular case, but imagine what happens if you have a pointer to a structure, a member of which is a pointer to another structure, a member of which is 'digit'. With the stars-and-parentheses form you'd have to write (*(*ptr1).ptr2).digit which is getting messy, while with -> you can just do ptr1->ptr2->digit Cheers, Richard -- Richard Stamp, Bournemouth, UK
|
Mon, 12 Mar 2001 03:00:00 GMT |
|
 |
Sunil Ra #10 / 13
|
 Help with pointers, and char -> int
Quote:
>> And, as I just found out, you can put the pointer in paranthesis... so >> my previous "*ptr.digit" should have been "(*ptr).digit". It works >> fine now. If anyone has any other comments, please respond about the >> "optimization" issues... >> BTW, I haven't understood the -> method... Please provide me with an >> example! >The -> is just a synonym for *. (star-dot). >Hence "ptr->digit" has exactly the same meaning as "(*ptr).digit". >-> is applied to a pointer to structures. >It is slightly shorter, arguably clearer ("arguably" means "I think so >but I couldn't actually back it up" :-)) and the usual "idiom" in C >(everybody writes C like that).
Its precedence is such that you can write expressions such as head->next->next without worrying about parens and stuff. Makes my code look a lot neater!!!! -- "I see you have books under your arm, brother. It is indeed a rare pleasure these days to come across somebody that still reads, brother." - Anthony Burgess
|
Mon, 12 Mar 2001 03:00:00 GMT |
|
 |
Stephan Wilm #11 / 13
|
 Help with pointers, and char -> int
Quote:
> it's down to the precedence of the * and . operators. Since . has higher > precedence, what is happening is this is viewed by the C compiler as > *(ptr.digit) > and since ptr is not a structure, but a pointer to one, you get the above > diagnostic. You can fix this by using the -> operator: > ptr->digit
Or by adding braces: (*ptr).digit Stephan (initiator of the campaign against grumpiness in c.l.c)
|
Tue, 13 Mar 2001 03:00:00 GMT |
|
|
|