By jotape


2012-11-06 14:28:08 8 Comments

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)

6 comments

@user5841469 2016-01-26 20:34:42

#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....

@Joe 2012-11-06 14:44:38

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)

@jotape 2012-11-06 15:03:42

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?

@Joe 2012-11-06 15:13:31

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]

@Anthony 2015-07-17 01:54:07

@Joe e.g., not i.e. /grammer-nazi

@John Bode 2012-11-06 15:01:08

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".

@Sravan 2012-11-06 14:51:14

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

@anishsane 2012-11-06 15:12:48

@Daveshaw: I wanted to post the same, but stackoverflow would not allow me to save edits < 6 chars of change :D

@SomeWittyUsername 2012-11-06 14:29:54

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

@md5 2012-11-06 14:29:20

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.

@md5 2012-11-06 14:40:08

Indeed. Note you can also use dynamic allocation (with a pointer to char), but it is useless here.

Related Questions

Sponsored Content

40 Answered Questions

6 Answered Questions

[SOLVED] What does "dereferencing" a pointer mean?

13 Answered Questions

[SOLVED] What is a smart pointer and when should I use one?

21 Answered Questions

[SOLVED] Why should I use a pointer rather than the object itself?

10 Answered Questions

[SOLVED] Improve INSERT-per-second performance of SQLite

11 Answered Questions

[SOLVED] How do function pointers in C work?

  • 2009-05-08 15:49:17
  • Yuval Adam
  • 805324 View
  • 1248 Score
  • 11 Answer
  • Tags:   c function-pointers

9 Answered Questions

[SOLVED] C pointers : pointing to an array of fixed size

  • 2009-11-27 18:20:17
  • figurassa
  • 62790 View
  • 118 Score
  • 9 Answer
  • Tags:   c pointers size

1 Answered Questions

[SOLVED] C strcmp() not returning 0 as expected

  • 2017-06-06 22:11:57
  • Ostküste
  • 617 View
  • 1 Score
  • 1 Answer
  • Tags:   c debugging strcmp

7 Answered Questions

[SOLVED] strcmp confusion

Sponsored Content