strcmp with pointers not working in C
Asked Answered
B

6

8

Why this code isn't working. Just trying to check if the user input is the same as a password

char *pass;

printf("Write the password: ");
scanf("%s", pass); // Because is a pointer the & is out ?


if( strcmp( pass , "acopio") == 0)
Buggy answered 6/11, 2012 at 14:28 Comment(1)
Why -1 ? I'm learning; tried to find the answer here; and for beginners it's not easy to understand other answers.Buggy
I
11

You've not actually allocated any space to put data. Defining a pointer just defines a variable that can hold the address of a block of data, it doesn't allocate the block.

You have a couple of options, allocate dynamic memory off the heap to write into and make the pointer point to it. Or use statically allocated memory on the stack and pass the address of it to your calls. There's little benefit to dynamic memory in this case (because it's temporary in use and small). You would have more work to do if you used dynamic memory - you have to make sure you got what you asked for when allocating it and make sure you've given it back when you're done AND make sure you don't use it after you've given it back (tricky in a big app, trust me!) It's just more work, and you don't seem to need that extra effort.

The examples below would also need significant error checking, but give you the general idea.

e.g.

char *pass = malloc (SOMESIZE);

printf("Write the password: ");
scanf("%s", pass);


if( strcmp( pass , "acopio") == 0)

or

char pass[SOMESIZE];

printf("Write the password: ");
scanf("%s", pass);


if( strcmp( pass , "acopio") == 0)
Intuitionism answered 6/11, 2012 at 14:44 Comment(3)
very clear. You forgot the & in the 2nd options scanf. Didn't get why is better to use dynamic memory(heap) than static memory(stack). Which is smaller?Buggy
You don't need the ampersand for the scanf call because referring to an array without any index is the address, i.e. pass == &pass[0]Intuitionism
@Intuitionism e.g., not i.e. /grammer-naziCrape
K
5

pass is an unitialized pointer, and you attempt to write into it. You have to allocate enough memory to hold a string. For example, char pass[SIZE] will work better.

Kulun answered 6/11, 2012 at 14:29 Comment(1)
Indeed. Note you can also use dynamic allocation (with a pointer to char), but it is useless here.Kulun
L
1

You need to allocate the pass so the scanf will have a place to store the input. Otherwise you have memory corruption.

Lycurgus answered 6/11, 2012 at 14:29 Comment(0)
L
0

Yes the pointer was not initialized. If you debug it you will get a access violation or segmentation fault. The code can be changed as follows.

 char pass[22];//22 can be replaced with other number

    printf("Write the password: ");
    scanf("%s", pass); 
    if( strcmp( pass , "acopio") == 0)
    printf("fu");//just to check
Loupe answered 6/11, 2012 at 14:51 Comment(1)
@Daveshaw: I wanted to post the same, but stackoverflow would not allow me to save edits < 6 chars of change :DErmaermanno
C
0

You haven't initialized pass to point to a buffer or other location to store the input.

For something simple like this, you can declare pass as an array of char instead of a pointer:

char pass[N]; // where N is large enough to hold the password plus a 0 terminator

scanf("%s", pass);
if (strcmp(pass, "acopio") == 0)
{
  ...
}

Except when it is the operand of the sizeof, _Alignof, or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array.

When you pass pass as an argument to scanf and strcmp, the type of the expression pass is converted from "N-element array of char" to "pointer to char", and the value of the expression is the address of the first element of pass, or &pass[0]. That's why you don't need to use the & operator in the scanf call.

Similarly, in the strcmp call, the string literal "acopio" is converted from an expression of type "7-element array of char" (const char in C++) to "pointer to char".

Coincide answered 6/11, 2012 at 15:1 Comment(0)
P
0
#include<stdio.h>
main()
{
    int mystrcmp(char *,char *);

    char s1[100],s2[100];
    char *p1,*p2;
    p1=s1;
    p2=s2;
    printf("Enter the first string..?\n");
    scanf("%s",p1);
    printf("Enter the second string..?\n");
    scanf("%s",p2);
    int x=mystrcmp(p1,p2);
    if(x==0)
        printf("Strings are same\n");
    else
        printf("Strings are not same..\n");


}
int mystrcmp(char *p1,char *p2)
{
    while(*p1==*p2)
    {
        if(*p1=='\0' || *p2=='\0')
            break;
        p1++;
        p2++;
    }
    if(*p1=='\0' &&as *p2=='\0')
        return(0);
    else
        return(1);
}

simple code for beginners....

Pe answered 26/1, 2016 at 20:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.