Thread: strcmp <string.h>

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    1

    strcmp <string.h>

    I don't know why program is crashing and on build message i have
    "warning: passing arg 1 of 'strcmp' makes pointer from integer without a cast"

    I want to compare first character of every string to "_" and if it is "_" exit the loop

    Here is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        int i,j;
        char a[20][4];
        char *pa[20];
        for (i=0; i<20; i++)
        {
            pa[i]=&a[i][0];
        }
        for (i=0; i<20; i++)
        {
            scanf("%s",&a[i][0]);
           if (strcmp(*(pa[i]+0),"_")==0)
            {
                printf("ok");
            }
        }
        for (i=0; i<20; i++)
        {
            for (j=0; j<4; j++)
            {
                printf("%c",*(pa[i]+j));
            }
        }
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > I don't know why program is crashing
    Because you ignored the build warning

    > and on build message i have
    > "warning: passing arg 1 of 'strcmp' makes pointer from integer without a cast"
    Because your attempt to pass a pointer with this nonsense *(pa[i]+0) resolves to an integer.

    All you needed was pa[i]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to optimise strcmp c-string comparison
    By Svetlin in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2010, 09:58 AM
  2. Using strcmp to find if a string ends in...
    By frankchester in forum C Programming
    Replies: 10
    Last Post: 12-09-2010, 08:09 PM
  3. struct, string, strcmp(), and "segmentation fault"
    By Roger in forum C Programming
    Replies: 10
    Last Post: 11-07-2009, 10:45 PM
  4. strcmp on a linked list to a string n
    By occams razor in forum C Programming
    Replies: 4
    Last Post: 03-31-2007, 08:54 AM
  5. strcmp
    By digy in forum C Programming
    Replies: 7
    Last Post: 05-15-2003, 11:49 AM

Tags for this Thread