Thread: Comparing string to arry of strings

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    Comparing string to arry of strings

    Hello there,

    I'm new to C programming and I have the following exercise to do:

    "Consider an array v[0, 1,..., (n-1)] whose elements are strings. Remember that, in C a string is just an array of chars ending with the null character '\0'. So, you can suppose that v was declared this way: char *v[n]. Write a function that receives a string x and returns a index j in which x = v[j]. If this j doesn't exist, the function must return -1."

    Here's what I did:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int contem(char string, char *v, int n) {
    
        int i;
    
        for (i = 0; i < 10; i++) {
            if (strcmp(v[i], string) == 0) {
                return i;
            }
        }
    
        return -1;
    
    }
    
    main() {
    
        char *v[10];
        v[0] = "File";
        v[1] = "Edit";
        v[2] = "View";
        v[3] = "Search";
        v[4] = "Tools";
        v[5] = "Documents";
        v[6] = "Help";
        v[7] = "Open";
        v[8] = "Save";
        v[9] = "Undo";
        
        char string [100];
    
        printf("String to search for: ");
        gets(string);
    
        int ret = contem(string, v, 10);
    
        if (ret == -1) {
                printf("The string is not in the array.");
                return 0;
        }
    
        else printf("The string is at position: ", ret);
    
        return 0;
    
    }
    I get the following warnings:

    warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast

    warning: passing argument 2 of 'strcmp' makes pointer from integer without a cast

    warning: passing argument 1 of 'contem' makes integer from pointer without a cast

    warning: passing argument 2 of 'contem' from incompatible pointer type



    What am I doing wrong? Does the array v must really be declared as a pointer?

    Thanks in advance.

    Regards,
    Samir

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the type of v[i]? What is the types of the parameters of strcmp?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It needs to be declared as an array of pointers (for that is what you're passing).

    int contem(char string, char *v[], int n)
    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.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    v is declared like this:
    char *v[10];

    After I have added the [] to the second argument of the contem function I got only two warnings:
    warning: passing argument 2 of 'strcmp' makes pointer from integer without a cast

    warning: passing argument 1 of 'contem' makes integer from pointer without a cast

    Is it that I am comparing a pointer to a char?
    Last edited by Samir Aguiar; 03-12-2012 at 11:52 PM.

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Samir Aguiar View Post
    Is it that I am comparing a pointer to a char?
    Yes indeed! Check what parameter types strcmp takes and then check what you're passing into it. Also the first parameter you're passing into contem() isn't right.
    Lastly, don't use gets() !
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Now I get it. v is declared as an array of pointers to char (char * v[]) while string is declared as an array of chars. I was passing to function contem v as a pointer to char and string as a char.
    I have changed contem arguments to this:

    int contem(char string[], char * v[], int n);

    And it solved the problem. Thanks for the help guys, I appreciate it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to select a string from an arry of strings
    By MNM(tm) in forum C Programming
    Replies: 13
    Last Post: 01-01-2012, 10:56 PM
  2. [HELP] about comparing between two strings...
    By kyaky in forum C++ Programming
    Replies: 12
    Last Post: 08-06-2007, 10:12 AM
  3. Comparing strings
    By manutd in forum C++ Programming
    Replies: 2
    Last Post: 11-08-2006, 06:49 PM
  4. comparing strings
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-23-2002, 11:20 AM
  5. Comparing 2 Strings
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-08-2002, 07:01 PM