tags:

views:

115

answers:

6
/*
 * PURPOSE
 *      Search if a string contains a string and print it out from there
 */
#include <stdio.h>

void searchHaystack(char cHaystack[], char cNeedle[]);
void showResult(int iOffset, char cHaystack[]);

int main() {
    // Declarations
    char cHaystack[50], cNeedle[50];

    // Input
    puts("Haystack:");
    gets(cHaystack);
    puts("Needle:");
    gets(cNeedle);

    // Call searcher
    searchHaystack(cHaystack, cNeedle);

    return 0;
}

void searchHaystack(char cHaystack[], char cNeedle[]) {
    // Declarations
    int iCntr, iCntr2, iFoundOffset;

    // Search the haystack for the first letter of the needle
    for (iCntr == 0; iCntr < 50 && cHaystack[iCntr] != '\0'; iCntr++) {
        if (cHaystack[iCntr] == cNeedle[0]) {
            iFoundOffset = iCntr;
            for (iCntr2 == 1; iCntr2 < 50 && (cHaystack[iCntr+iCntr2] == cNeedle[iCntr2] || cNeedle[iCntr2] == '\0'); iCntr2++) {
                if (cNeedle[iCntr2] == '\0') {
                    showResult(iFoundOffset, cHaystack);
                }
            }
        }
    }
}

void showResult(int iOffset, char cHaystack[]) {
    int iCntr;

    // Print the substring char by char
    for (iCntr == iOffset; iCntr < 50 && cHaystack[iCntr] != '\0'; iCntr++) {
        printf("%c", cHaystack[iCntr]);
    }
    printf("\n");
}

Looking at my debugger I noticed that cHaystack[] and cNeedle[] aren't passed to searchHaystack properly as only the first char is conserved. How do I fix this? I haven't learned about pointers yet.

Also, I'm getting this warning on all three for loops:

statement with no effect

What's up with that?

+1  A: 

These are character arrays not strings. In C string are of type char * and you have to allocate the memory for them.

Of course when you say varname[5] that is the same as saying *(varname+5)

Basically you are going to have to learn about pointers to use strings in C.

EDIT

As pointed out below (and above by me) you can use character arrays like strings in C. HOWEVER, my point is that if you don't learn a little bit about pointers you are going to be in big trouble.

For example:

  • Not being able to view the string in the debugger.

  • Not putting a null as the last character in the array and having crazy random bugs

  • Forgetting that you only allocated X bytes for the array and going over the end

etc.

If you don't understand how pointers work in C, it is really hard -- if not impossible to work with the language.

I expect the prof will cover it next week.

Hogan
char* is equivalent to char[], and he declares both variables as char[50]. As long as both inputs are less than 50 characters, he's fine.
Asher Dunn
char a[]; is the same thing as char *a; The arrays are allocated on the stack in main()
Jason Williams
+2  A: 

Actually, the entire array IS being passed, the debugger only shows the first char by default because in C, the system does not know the size of an array. It is something the program has to keep track of. Since you are using strings though, which are typically null terminated, try setting the watch variable "(char*)cHaystack" (without quotes) and see what the debugger shows then.

Also, assignment statements should have one = sign, not the double == sign. So:

for (iCntr = 0; ...

Should be used, NOT:

for (iCntr == 0; ...

Same with the other for loops.

Walt W
D'oh. The == thing was a stupid mistake and solved the problem.
Pieter
+1  A: 

You're starting the loop with iCntr == 0 This is a comparison, so it does not set iCntr to zero.

Use iCntr = 0 (a single equals sign)

Jason Williams
Anyone care to explain the downvote?
Jason Williams
A: 

The warning is probably caused by

iCntr == 0,iCntr2 == 1, iCntr == iOffset

I guess you were going, in fact, for:

iCntr = 0,iCntr2 = 1, iCntr = iOffset

As for passing the arrays, you could do something like ( using pointers ):

void searchHaystack(char* cHaystack, int cHaystackSize, char* cNeedle, int cNeedleSize )
...
  for (iCntr = 0; iCntr < cHaystackSize && cHaystack[iCntr] != '\0'; ++iCntr )
Iustin Amihaesei
+1  A: 

The values are passed properly, but your expectation of how your debugger should display them is incorrect. As already mentioned, there is no string type in C. Instead, C uses char* variables -- pointers to characters; your char[] are equivalent to char*.

You know that the pointed-to character is the first character in a longer string, but the debugger doesn't. It displays the character that the pointer points to -- which you know to be the first of a longer string. The debugger, however, only knows it's a char*, and there must be a char to be pointed at, so it displays that char.

Asher Dunn
+1  A: 

Arrays are not first-class objects in C; when you pass an array as a function parameter, the type of the array expression is implicitly converted from "N-element array of T" to "pointer to T", and its value is set to point to the first element in the array[1].

In the context of a function parameter declaration, int a[] is the same as int *a (but this is true only in the context of a function parameter declaration); your searchHaystack function receives two pointers to char, which correspond to the first elements of the respective arrays. The debugger doesn't show you the whole array, because in the context of the function they are not arrays.

Also, NEVER, NEVER, NEVER USEgets(). Ever. It will introduce a point of failure in your code. Use fgets() instead. C does no bounds checking on arrays. If you call gets() for a buffer sized to hold 10 characters, and the user types in 100 characters, gets() will happily store those extra 90 characters in the memory following your buffer, potentially clobbering something important and leading to a crash or worse (buffer overruns are a common exploit for malware; the Morris worm exploited a call to gets() in sendmail).

The warning is coming from you using == instead of = to assign your loop counters.

  1. The exceptions to this rule are when the array expression is an operand of either the sizeof or address-of (&) operators, or when the array is a string literal being used to initialize another array in a declaration.
John Bode
+1 for warning about dangers of `gets()`.
dreamlax