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!
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!
That doesn't check if they're null.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 ==.Code:char *p1 = NULL; char *p2 = NULL; if( p1 == p2 ) printf( "Yay! ... er ... wait. They're still null.\n" );
Quzah.
Hope is the first step on the road to disappointment.
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;
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
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;
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.
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; }
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; }
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 }
ok, now I grasp... I fixed it. Thanx a lot!