Testing Pointers 
Author Message
 Testing Pointers

I am hoping someone out there can help with a problem that I am having.
I have a program in which I want to test the pointer that was past to
it. Here is an example:

void main()
{
    foo("Hello World");

Quote:
}

void foo(*testvar)
{
    if(testvar == "Hello World")
        printf(" It Worked");

Quote:
}

If you can reply by E-Mail that would be great. Again thanks for the
help.


Fri, 07 Jan 2000 03:00:00 GMT  
 Testing Pointers


Quote:
> I am hoping someone out there can help with a problem that I am having.
> I have a program in which I want to test the pointer that was past to
> it. Here is an example:

> void main()
> {
>     foo("Hello World");
> }

> void foo(*testvar)
> {
>     if(testvar == "Hello World")
>         printf(" It Worked");
> }

Comparing a pointer testvar to a string literal, "Hello World", will not
generally work. What you need to do is test whay the point to. Do the two
strings match? Use strcmp() to find out.

#include <string.h>

void foo(char *testvar)
{
    if(!strcmp(testvar, "Hello World"))
       puts("It worked");

Quote:
}



Fri, 07 Jan 2000 03:00:00 GMT  
 Testing Pointers



Quote:
>I am hoping someone out there can help with a problem that I am having.
>I have a program in which I want to test the pointer that was past to
>it. Here is an example:

>void main()

Since main returns int int in C that should be:

int main()

or better still:

int main(void)

Quote:
>{
>    foo("Hello World");
>}

>void foo(*testvar)
>{
>    if(testvar == "Hello World")

You are testing the pointer here. I guess however that what you really
want to test is the string that the pointer is pointing to. In that case
use:

     if(strcmp(testvar, "Hello World") == 0)

and don't for get to #include <string.h> at the top.

Quote:
>        printf(" It Worked");

You also need to #include <stdio.h> to use printf().

    return 0;

Quote:
>}
>If you can reply by E-Mail that would be great. Again thanks for the
>help.

It would be even better if you checked for replies in the newsgroup.

--
-----------------------------------------


-----------------------------------------



Fri, 07 Jan 2000 03:00:00 GMT  
 Testing Pointers

[This followup was posted to comp.lang.c and a copy was sent to the cited
author.]


says...

Quote:
> I am hoping someone out there can help with a problem that I am having.
> I have a program in which I want to test the pointer that was past to
> it. Here is an example:
> void main()
> {
>     foo("Hello World");
> }
> void foo(*testvar)
> {
>     if(testvar == "Hello World")
>         printf(" It Worked");
> }

Good try, but you'll never get any cigars this way; you're comparing a
pointer to a string literal - this will never work.

You need to get out your manual and look up strcmp:

if ( strcmp(  testvar, "Hello World" ) == 0 )
        printf( " It Worked" );

        -- AK

--



Fri, 07 Jan 2000 03:00:00 GMT  
 Testing Pointers


DCP> I am hoping someone out there can help with a problem that I am having.
DCP> I have a program in which I want to test the pointer that was past to
DCP> it. Here is an example:
DCP>
DCP> void main()

main() returns int, not void.

DCP> {
DCP>     foo("Hello World");

foo() is undeclared at this point.  You should put a function
prototype before main().

DCP> }
DCP>
DCP> void foo(*testvar)

This is completely meaningless; what type of pointer is testvar?  You
probably meant to say void foo(char *testvar) instead.

DCP> {
DCP>     if(testvar == "Hello World")

This is legal but not what you want; it compares the pointers to two
distinct strings (incidentally both containing the text "Hello World")
rather than comparing their contents.  Look at the strcmp() library
function.

DCP>         printf(" It Worked");
DCP> }
DCP>
DCP> If you can reply by E-Mail that would be great. Again thanks for the
DCP> help.

(On general principle, I will post to USENET as well.)

--
 _____________________________
/                             \  "We can write all we know in 4 lines."
|          David Maze         |   -- Ray Ashoori, MIT Physics Professor

| http://donut.mit.edu/dmaze/ |        IHTFP         17 Yellow Pigs
\_____________________________/     ...the Institute is your friend...



Fri, 07 Jan 2000 03:00:00 GMT  
 Testing Pointers


Quote:
>I am hoping someone out there can help with a problem that I am having.
>I have a program in which I want to test the pointer that was past to
>it. Here is an example:
>void main()
>{
>    foo("Hello World");
>}
>void foo(*testvar)
>{
>    if(testvar == "Hello World")
>        printf(" It Worked");
>}

Depending on your compiler, this may or may not print " It Worked".
Remember, the string "Hello World" is treated as a pointer by C, to a
place that contains the letters of that message and a terminating '\0'.
Just because the string "Hello World" is the same in both places doesn't
mean that your compiler has to make both pointers point to the same place
in memory. The "Hello World" could be repeated, and one pointer points to
one copy of those characters in memory, and the other pointer to another.

If you want to see if the string points to a particular set of characters,
you might want to look at the standard C functions, strcmp() and
strncmp(). Some systems (I'm not sure if it's ANSI or not) supply a
stricmp(), as well. If you're trying to find out if the char pointer has
a particular value, you're going to have to rely on something
non-portable, or to comparing with a known, global pointer of that value.

  Bob
--
  Bob           One good thing about being wrong is the joy it brings to others



Fri, 07 Jan 2000 03:00:00 GMT  
 Testing Pointers

This is not Pascal language!
Whe you write if(testvar == "Hello World"), the compiler will generates
code to test if testvar contains the same address that the one where "Hello
World" is stored!  I believe that you wanted to test if testvar points to a
string 'equal to' "Hello World". To do this, you must use
if(strcmp(testvar,"Hello Worl) == 0).
BTW, the type of parameter to foo should be char * instead of *.




Quote:
> I am hoping someone out there can help with a problem that I am having.
> I have a program in which I want to test the pointer that was past to
> it. Here is an example:

> void main()
> {
>     foo("Hello World");
> }

> void foo(*testvar)
> {
>     if(testvar == "Hello World")
>         printf(" It Worked");
> }

> If you can reply by E-Mail that would be great. Again thanks for the
> help.



Sat, 08 Jan 2000 03:00:00 GMT  
 Testing Pointers


Quote:

>[This followup was posted to comp.lang.c and a copy was sent to the cited
>author.]


>says...

>> I am hoping someone out there can help with a problem that I am having.
>> I have a program in which I want to test the pointer that was past to
>> it. Here is an example:

>> void main()
>> {
>>     foo("Hello World");
>> }

>> void foo(*testvar)
>> {
>>     if(testvar == "Hello World")
>>         printf(" It Worked");
>> }

>Good try, but you'll never get any cigars this way; you're comparing a
>pointer to a string literal - this will never work.

"Never" is perhaps going a little far.  There is one (unfortunate)
circumstance in which it would work, perhaps confusing the hell out
of someone who had written a little test program to establish
whether it would work.

On the whole though, it is unlikely to work, won't work if the two
strings aren't both constant, and should be avoided.

John
--
John Winters.  Wallingford, Oxon, England.



Sat, 08 Jan 2000 03:00:00 GMT  
 Testing Pointers

#include <stdio.h>
void foo(char *testvar);

int main( void )
{
    foo("Hello World");

Quote:
}

void foo(char *testvar)
{
    if(testvar == "Hello World")
        printf(" It Worked\n");

Quote:
}

Output:

 It Worked

--------
I don't have a C book, so I can't really look anything up, but I have the
  faq :), so please correct anything thats broken... thanks

For testing pointers, this certainly works for gcc anyway.

gcc uses 1 (one) copy of the string "Hello World", even though there are two
  litteral strings in the program.

The call foo("Hello World") passes a copy of the pointer to the string.
Since everything is pointing to the same string, it prints out It worked.

But if you want to actually compare them...

This is probably relevant:

8.2:    I'm checking a string to see if it matches a particular value.
        Why isn't this code working?

                char *string;
                ...
                if(string == "value") {
                        /* string matches "value" */
                        ...
                }

A:      Strings in C are represented as arrays of characters, and C
        never manipulates (assigns, compares, etc.) arrays as a whole.
        The == operator in the code fragment above compares two pointers
        -- the value of the pointer variable string and a pointer to the
        string literal "value" -- to see if they are equal, that is, if
        they point to the same place.  They probably don't, so the
        comparison never succeeds.

        To compare two strings, you generally use the library function
        strcmp():

                if(strcmp(string, "value") == 0) {
                   /* strings are equal. */
                }

                                                                azbok

I like the campaign against grumpness in the c.l.c.
Here's mine:

(Azboklin De Lirik, in the campaign to promote HAPPINESS in the c.l.c.)



Sat, 08 Jan 2000 03:00:00 GMT  
 Testing Pointers



        [much snip]

Quote:
> For testing pointers, this certainly works for gcc anyway.

> gcc uses 1 (one) copy of the string "Hello World", even though there are
two
>   litteral strings in the program.

The C standard does not require merging duplicate strings.  Some compilers
do not do it at all, some only do it as an option with a command line
switch of #pragma, so expecting this to work in non-portable.

--

Jack Klein

All views expressed in this message are mine,
and not necessarily that of my company or any
of our clients.



Sun, 09 Jan 2000 03:00:00 GMT  
 Testing Pointers

Below you are testing the memory locations of two pointers.
Both pointers point to string literals, and since the two literals
aren't in the exact
same location, the test will fail.

Quote:

> I am hoping someone out there can help with a problem that I am
> having.
> I have a program in which I want to test the pointer that was past to
> it. Here is an example:

> void main()
> {
>     foo("Hello World");
> }

> void foo(*testvar)
> {
>     if(testvar == "Hello World")
>         printf(" It Worked");
> }

> If you can reply by E-Mail that would be great. Again thanks for the
> help.



Mon, 17 Jan 2000 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Need to test pointer to object before deleting it

2. test test test test

3. test...test...test

4. Test Result (argv vs mystring pointer)?

5. Testing for junk pointers...?

6. test wether a pointer to a structure is valid

7. pointers, tests, casts

8. Test a pointer before de-referencing

9. How do you test for invalid pointers in ATL

10. How best to test a valid pointer?

11. How do you test for invalid pointers in ATL

12. How do you test for invalid pointers in ATL

 

 
Powered by phpBB® Forum Software