Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer arithmetic error?

Reply
Thread Tools

pointer arithmetic error?

 
 
Bill Cunningham
Guest
Posts: n/a
 
      12-04-2009
I am creating a dice game that returns ints up to the number specified
by argv[1]. Simple enough. But I am also wanting to add a switch, "-a" to be
able to run a routine that adds two numbers and returns the result. So I
want the program to accept "-a" or "a" or "a-" as the switch. Which would be
*(argv[1]) *(argv[1]+1) or *(argv[1]+2) that I would need C to examine. Or
argv[1][0] argv[1][1] argv[1][2].

But I was going to try it through pointer arithmetic.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

int main(int argc, char *argv[])
{
if (argc > 4 || argc == 0) {
fprintf(stderr, "dice usage error\n");
exit(EXIT_FAILURE);
}
int c, d, x, y;
char **ptr = &argv[1];
x = strtol(argv[2], NULL, 10);
y = strtol(argv[3], NULL, 10);
if (*(ptr) || *(ptr + 1) == 'a') {
printf("%i\n", x + y);
exit(0);
}

di.c: In function `main':
di.c:16: warning: comparison between pointer and integer
di.c:19: error: syntax error at end of input

Errors from running gcc -c di.c

Bill


---
In C there should be no or little need for casts

--Rihard Heathfield


 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      12-04-2009
On December 4, 2009 15:40, in comp.lang.c, Bill Cunningham
((E-Mail Removed)) wrote:

> I am creating a dice game that returns ints up to the number specified
> by argv[1]. Simple enough. But I am also wanting to add a switch, "-a" to
> be able to run a routine that adds two numbers and returns the result. So
> I want the program to accept "-a" or "a" or "a-" as the switch. Which
> would be *(argv[1]) *(argv[1]+1) or *(argv[1]+2) that I would need C to
> examine. Or
> argv[1][0] argv[1][1] argv[1][2].
>
> But I was going to try it through pointer arithmetic.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <ctype.h>
> #include <time.h>
>
> int main(int argc, char *argv[])
> {
> if (argc > 4 || argc == 0) {
> fprintf(stderr, "dice usage error\n");
> exit(EXIT_FAILURE);
> }
> int c, d, x, y;
> char **ptr = &argv[1];
> x = strtol(argv[2], NULL, 10);
> y = strtol(argv[3], NULL, 10);
> if (*(ptr) || *(ptr + 1) == 'a') {


OK, Bill. Above is line 16, the origin of the "comparison between pointer
and integer" warning.

Do we agree that <ptr> is a pointer to a pointer to char? Yes?
So <*(ptr)> is a pointer to char, right? We dereference a pointer to
pointer, to get a pointer.

And < *(ptr + 1) > is also a pointer to char, right? We add 1 to <ptr>,
getting another "pointer to a pointer to char", then dereference it to get
a "pointer to char".

But, what of <'a'>? What type of data is it? Of course, <'a'> is an integer
constant.

Now, in line 16, you compare
a pointer to char
with
an integer constant
but, of course, these two things have different types, and cannot be
compared with any meaning.

Hence the warning message.

> printf("%i\n", x + y);
> exit(0);
> }


Above is line 19. The line contains the closing brace of a compound
statement, and that brace matches the opening brace on line 16.

There is one other compound statement that is still open at this point: the
compound statement that started on line 7 (the 'body' statement of the
main() function).

Presumably, your source file ends here. Since no closing brace (to match the
opening brace on line 7) has been found by the end of the source file, the
compiler complains that you have a syntax error.

> di.c: In function `main':
> di.c:16: warning: comparison between pointer and integer
> di.c:19: error: syntax error at end of input
>
> Errors from running gcc -c di.c
>
> Bill
>
>
> ---
> In C there should be no or little need for casts
>
> --Rihard Heathfield
>
>


--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      12-05-2009

"Lew Pitcher" <(E-Mail Removed)> wrote in message
news:cfc8a$4b19771c$cef882ed$(E-Mail Removed)-Free...

> But, what of <'a'>? What type of data is it? Of course, <'a'> is an
> integer
> constant.


[snip]

'a' is what is supposed to be in argv[1]. So I would think it would be a
char* or char**. To be more precise I want it in argv[1][1] or argv[1][0] is
acceptable. The code as you correctly noticed is a fragment. From the
command line if -a or a is entered as argv[1] I would use x and y total the
values for argv[3] and argv[4].

I'm not quite seeing where you say a is an int. I got to learn this
sometime.

Bill
---
In C there should be no or little need for casts

--Richard Heathfield


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-05-2009
"Bill Cunningham" <(E-Mail Removed)> writes:
> "Lew Pitcher" <(E-Mail Removed)> wrote in message
> news:cfc8a$4b19771c$cef882ed$(E-Mail Removed)-Free...
>
>> But, what of <'a'>? What type of data is it? Of course, <'a'> is an
>> integer
>> constant.

>
> [snip]
>
> 'a' is what is supposed to be in argv[1].


No.

argv is a char**, so argv[1] is of type char*.

> So I would think it would be a
> char* or char**.


Why would you think that? How could 'a' be of type char* or char**?

I can understand thinking that 'a' is of type char. For historical
reasons, character constants such as 'a' are of type int, not of type
char. It could make sense to compare a character constant to
something of type char.

> To be more precise I want it in argv[1][1] or argv[1][0] is
> acceptable.


That's not more precise, that's just different.

argv is of type char**. argv[1] is of type char*. argv[1][1] is of
type char. Three different types, none of which are in any way
compatible with each other.

> The code as you correctly noticed is a fragment.


The only thing that kept it from being a complete program was the
closing "}" for main.

> From the
> command line if -a or a is entered as argv[1] I would use x and y total the
> values for argv[3] and argv[4].
>
> I'm not quite seeing where you say a is an int. I got to learn this
> sometime.


There's nothing in your program named a. 'a', the character constant,
is of type int. (a is an identifier; 'a' is a character constant.
This is not a trivial difference.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      12-05-2009

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> I can understand thinking that 'a' is of type char. For historical
> reasons, character constants such as 'a' are of type int, not of type
> char. It could make sense to compare a character constant to
> something of type char.
>
>> To be more precise I want it in argv[1][1] or argv[1][0]
>> is
>> acceptable.

>
> That's not more precise, that's just different.
>
> argv is of type char**. argv[1] is of type char*. argv[1][1] is of
> type char. Three different types, none of which are in any way
> compatible with each other.
>
>> The code as you correctly noticed is a fragment.

>
> The only thing that kept it from being a complete program was the
> closing "}" for main.
>
>> From the
>> command line if -a or a is entered as argv[1] I would use x and y total
>> the
>> values for argv[3] and argv[4].
>>
>> I'm not quite seeing where you say a is an int. I got to learn this
>> sometime.

>
> There's nothing in your program named a. 'a', the character constant,
> is of type int. (a is an identifier; 'a' is a character constant.
> This is not a trivial difference.)


So what should I do? I thought 'a' meant character. Cast? I don't think
that would be the right thing to do. Should I use some kind of conversion
function? I'm stumped on this one.

Bill
---
In C there should be no or little need for casts

--Richard Heathfield



 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-05-2009
"Bill Cunningham" <(E-Mail Removed)> writes:

> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> I can understand thinking that 'a' is of type char. For historical
>> reasons, character constants such as 'a' are of type int, not of type
>> char. It could make sense to compare a character constant to
>> something of type char.
>>
>>> To be more precise I want it in argv[1][1] or argv[1][0]
>>> is
>>> acceptable.

>>
>> That's not more precise, that's just different.
>>
>> argv is of type char**. argv[1] is of type char*. argv[1][1] is of
>> type char. Three different types, none of which are in any way
>> compatible with each other.
>>
>>> The code as you correctly noticed is a fragment.

>>
>> The only thing that kept it from being a complete program was the
>> closing "}" for main.
>>
>>> From the
>>> command line if -a or a is entered as argv[1] I would use x and y total
>>> the
>>> values for argv[3] and argv[4].
>>>
>>> I'm not quite seeing where you say a is an int. I got to learn this
>>> sometime.

>>
>> There's nothing in your program named a. 'a', the character constant,
>> is of type int. (a is an identifier; 'a' is a character constant.
>> This is not a trivial difference.)

>
> So what should I do? I thought 'a' meant character. Cast? I don't think
> that would be the right thing to do. Should I use some kind of conversion
> function? I'm stumped on this one.


The fact that the types are wrong is just showing you that you have
not found the right thing to compare equal to 'a'. argv is of type
char ** and argv[1] is of type char *. When your program is called
with argv[1] pointing at "-a" then argv[1][0] == '-' and argv[1][1] ==
'a'. Alternatively you can test to see of strcmp(argv[1], "-a") == 0.

> ---


This is not a good sig separator. The accepted standard is "-- "
(note the space after the two dashes).

--
Ben.
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      12-05-2009
On 5 Dec, 03:40, "Bill Cunningham" <(E-Mail Removed)> wrote:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...


> > I can understand thinking that 'a' is of type char. *For historical
> > reasons, character constants such as 'a' are of type int, not of type
> > char. *It could make sense to compare a character constant to
> > something of type char.


note well.

> >> To be more precise I want it in argv[1][1] or argv[1][0]
> >> is acceptable.

>
> > That's not more precise, that's just different.

>
> > argv is of type char**. *argv[1] is of type char*. *argv[1][1] is of
> > type char. *Three different types, none of which are in any way
> > compatible with each other.


did you follow that?

> >> *The code as you correctly noticed is a fragment.

>
> > The only thing that kept it from being a complete program was the
> > closing "}" for main.

>
> >> From the
> >> command line if -a or a is entered as argv[1] I would use x and y total
> >> the
> >> values for argv[3] and argv[4].

>
> >> * * I'm not quite seeing where you say a is an int. I got to learn this
> >> sometime.

>
> > There's nothing in your program named a. *'a', the character constant,
> > is of type int. *(a is an identifier; 'a' is a character constant.
> > This is not a trivial difference.)

>
> * * So what should I do? I thought 'a' meant character.


yes 'a' is character constant (ignore for the time being that it is of
type int) and can be compared with a char.

char c;
if (c == 'a')

is perfectly ok (assuming c has a sensible value in it). You are
trying to compare a char constant with a POINTER-TO-CHAR. THis is not
valid

char *pc;
if (pc == 'a')

this is NOT ok


> Cast? I don't think
> that would be the right thing to do. Should I use some kind of conversion
> function? I'm stumped on this one.


explain what you are trying to do.

Would this do what you want?

if (argv[1][0] == 'a')




 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      12-05-2009

"Nick Keighley" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On 5 Dec, 03:40, "Bill Cunningham" <(E-Mail Removed)> wrote:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...


> > I can understand thinking that 'a' is of type char. For historical
> > reasons, character constants such as 'a' are of type int, not of type
> > char. It could make sense to compare a character constant to
> > something of type char.


note well.

> >> To be more precise I want it in argv[1][1] or argv[1][0]
> >> is acceptable.

>
> > That's not more precise, that's just different.

>
> > argv is of type char**. argv[1] is of type char*. argv[1][1] is of
> > type char. Three different types, none of which are in any way
> > compatible with each other.


did you follow that?

I think.

> >> The code as you correctly noticed is a fragment.

>
> > The only thing that kept it from being a complete program was the
> > closing "}" for main.

>
> >> From the
> >> command line if -a or a is entered as argv[1] I would use x and y total
> >> the
> >> values for argv[3] and argv[4].

>
> >> I'm not quite seeing where you say a is an int. I got to learn this
> >> sometime.

>
> > There's nothing in your program named a. 'a', the character constant,
> > is of type int. (a is an identifier; 'a' is a character constant.
> > This is not a trivial difference.)

>
> So what should I do? I thought 'a' meant character.


yes 'a' is character constant (ignore for the time being that it is of
type int) and can be compared with a char.

char c;
if (c == 'a')

Ok but that wouldn't work with two characters. For example this I know
wouldn't work:

char c;
if( c=="-a")

Because it's a string. I was going to try my hand at pointer arithmetic and
look at argv[1][0] and argv[1][1].

is perfectly ok (assuming c has a sensible value in it). You are
trying to compare a char constant with a POINTER-TO-CHAR. THis is not
valid

char *pc;
if (pc == 'a')

this is NOT ok


> Cast? I don't think
> that would be the right thing to do. Should I use some kind of conversion
> function? I'm stumped on this one.


explain what you are trying to do.

Would this do what you want?

if (argv[1][0] == 'a')


The program dice.exe would be called from DOS or windows command by dice -a
4 5 and the result would be 9. Now it should also do this as a number
generator:

dice 6 //would give randoms up to 6.
dice 12 //would give randoms up to 12.

Bill

--
In C there should be no or little need for casts

--Richard Heathfield



 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      12-05-2009

"Nick Keighley" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

Would this do what you want?

if (argv[1][0] == 'a')


Yes that would work. I was trying to attempt this via this way. Maybe it
doesn't work that way.

*(argv[1]) and *(argv[1]+1) which would a pointers to argv[1][0] and
argv[1][1].

Bill

--
In C there should be no or little need for casts

--Richard Heathfield



 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      12-05-2009

"Bill Cunningham" <(E-Mail Removed)> wrote in message
news:4b1a9943$0$5330$(E-Mail Removed). ..
>
> "Nick Keighley" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> Would this do what you want?
>
> if (argv[1][0] == 'a')


Wait more what I woant come to think of it is

if (argv[1][0]=='-' && argv[1][1]=='a')

> Yes that would work. I was trying to attempt this via this way. Maybe it
> doesn't work that way.
>
> *(argv[1]) and *(argv[1]+1) which would a pointers to argv[1][0] and
> argv[1][1].
>
> Bill
>
> --
> In C there should be no or little need for casts
>
> --Richard Heathfield
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments