Compiler warning when dealing with strings

This is a discussion on Compiler warning when dealing with strings within the C Programming forums, part of the General Programming Boards category; Hey guys, I am supposed to find a substring in a string of characters.Here is my code: #include<stdio.h> #include<string.h> #include<stdlib.h> ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    9

    Compiler warning when dealing with strings

    Hey guys,
    I am supposed to find a substring in a string of characters.Here is my code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int find(char *a, char *key);
    int main(void)
    {
    char string[30];
    char substr[10];
    char exist;

    printf("Enter the string: ");
    gets(string);
    printf("Enter the substring: ");
    gets(substr);

    exist = find(string,substr);
    if (exist != NULL)
    printf("Found that part in original string\n");
    else
    printf("Have not found that part in original string\n");

    return 0;
    }
    int find(char *a, char *key)
    {
    while(*a != '\0'){
    if(*a == *key)
    return (1);
    else
    return (0);
    }
    }

    Now, it seems to work fine but I get this warning from Borland C++: Function should return a value in function find.

    I thought it did return a value which is either a pointer to a substring or a NULL pointer.Ideas?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > Function should return a value in function find.
    If your while loop executes zero times, then the function just falls off the end with no return statement, since both return statements are inside the while loop

    It also only compares the first char - there is no a++ or key++ to compare successive chars

    > I thought it did return a value which is either a pointer to a substring or a NULL pointer.Ideas?
    Looks like 0 or 1 to me

    If you want to return a pointer, then the function should return a char *

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. compiler linkage warning
    By stanlvw in forum C++ Programming
    Replies: 2
    Last Post: 06-05-2008, 01:18 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Replies: 18
    Last Post: 12-31-2005, 12:56 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21