if statement and pointers

This is a discussion on if statement and pointers within the C Programming forums, part of the General Programming Boards category; Hi all! How to write an if statement that checks if two pointers point at equal arrays? This just check ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    61

    if statement and pointers

    Hi all!

    How to write an if statement that checks if two pointers point at equal arrays? This just check if the pointers are not NULL:

    if (pointer == pointer)

    Thanks in advance!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That doesn't check if they're null.
    Code:
    char *p1 = NULL;
    char *p2 = NULL;
    
    if( p1 == p2 )
        printf( "Yay! ... er ... wait. They're still null.\n" );
    What exactly are you trying to compare? You can compare if two pointers point to the same spot in memory, as you've done above. But you can't compare two different spots in memory to see if what they point to has the same value--unless you do it one element at a time. That is to say, you cannot compare entire strings or blocks of memory (arrays, multi-element dynamically allocated blocks) using ==.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    The pointers are two strings and I want to compare them. I suppose that I should use the string.h library. But this code doesn't word:

    Code:
    	int v;
    	v = strcmp( pointer1, pointer2 );
    	if (v == 0);
    				save=1;

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Pointer1 and Pointer2, most probably are pointers(addresses which are just numbers), so how can you use them with strcmp?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    Thanks for the comments!

    This is my final solution that seems to work:
    Code:
    	strcpy(array1, pointer1);
    	strcpy(array2, pointer2);
    	v=strcmp(array1, array2);
    	if (v == 0)
    			save=1;

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    int strcmp ( const char * str1, const char * str2 );
    http://www.cplusplus.com/reference/c...string/strcmp/
    Last edited by since; 12-15-2009 at 11:20 AM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    You cannot compare arrays using strcmp.
    Also note your bug in your previous code where you had a ; after the if.
    To compare if arrays are equal, you could use memcmp.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    hmmm... but it doesn't wok the way I want -- save is not 1 even though array1 seems to be equal array2.

    here is the code:
    Code:
    struct node* Build(struct node* p)
    {
    	char array1[80];
    	char array2[80];
    	pointer1=strtok(buf, " ");
    	while(pointer1)
    	{
    		p->word=malloc(80);
    		strcpy(p->word, pointer1);
    		strcpy(array1, pointer1);
    		strcpy(array2, pointer2);
    		if(!strcmp(array1, array2))
    		{
    			save=1;
    		}
    		n=malloc(sizeof(struct node));
    		pointer1=strtok(NULL, " ");
    		p->next=n;
    		p=n;
    	}
    	return p;
    }

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    Thanks Elysia!

    ...but this code seems not to work, even though I suppose it's nearer the final solution

    Code:
    if(!(memcmp(array1, array2, strlen(array2))))
    {
    	save=1;
    }

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Small example.
    Code:
    #include <iostream>
    #include <memory.h>
    #include <string.h>
    
    int main()
    {
    	int n1[3] = { 1, 2, 3 };
    	int n2[3] = { 1, 2, 3 };
    	char buf1[] = "These strings are equal";
    	char buf2[] = "These strings are equal";
    	if (memcmp(n1, n2, sizeof(n1)) == 0)
    		// They are equal
    	if (memcmp(buf1, buf2, sizeof(n1)) == 0)
    		// They are equal
    	if (strcmp(buf1, buf2) == 0)
    		// They are equal
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    ok, now I grasp... I fixed it. Thanx a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble understanding function pointers...
    By fitzpatrickbt in forum C Programming
    Replies: 5
    Last Post: 09-15-2009, 12:56 AM
  2. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  3. SQLExecute failing, Need Some Pointers
    By WaterNut in forum C++ Programming
    Replies: 4
    Last Post: 03-15-2006, 10:49 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 12:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21