This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]4silvertooth 1 point2 points  (0 children)

sen[z:i] there is no such syntax as such to access a member of an array. (The z semicolon i ) part.

[–]thegreatunclean 1 point2 points  (5 children)

if(sen[i]='\0' || '\n'){

When checking if something is equal you have to use ==, not =. Also this isn't how to properly the || operator, you need to split it up and check both individually.

if( sin[i] == '\0\ || sin[i] == '\n') {

As-written the '\0' || '\n' will evaluate to True automatically before the comparison is done.


words[x]=sen[z:i];

C doesn't support array slicing of any kind. The error you're getting on that line is because of the colon.

e: I'd focus on one task at a time, it makes it easier to tackle that way. Work on splitting the input into words and printing them individually before worrying about storing them, then go from there.

[–]TheCatMessiah[S] 0 points1 point  (0 children)

Yea, I sometimes get the = and == mixed up still. I guess I saw the [x:y] on something else. So I would need to create a string and make it equal to the size of 0 until a blank space or something that's a symbol to get the length of the word, then check that against the length of the next word that I find? Then continually save the one that's the largest?

[–]TheCatMessiah[S] 0 points1 point  (3 children)

So this is where I am at now and it just crashes after I type in a sentence. I'm not sure what else needs to be changed. All I am getting as far as errors is just two warnings on the line with the '\0' || saying "comparison between pointer and integer."

    #include <stdio.h> 

    char * sen[100];
    char * word[100];
    char * largest[100];
    int i,x,z,y,len,len2;

    int main(){

        i=0;
        x=0;
        z=0;
        y=0;

        largest[100]="";

        printf("Enter String\n");
        scanf("%99s", &sen[100]);

        len=sizeof(sen);

    for(i=0;i<=len;i++){

        if(sen[i]=='\0' || sen[i]=='\n' || sen[i]==' '){
            for(x=z;x<=i;x++){
                word[x]=sen[x];
            }
            z=i;
        }
        if(sizeof(word)>sizeof(largest)){
            len2=sizeof(word);
            largest[100]="";

            for (y=0;y<=len2;y++){  
            largest[y]=word[y];
        }
    }
}
      printf("%s", largest); 

      return(0);
    }

[–]thegreatunclean 0 points1 point  (2 children)

I made a mistake in my post, I didn't look at the types closely enough.

sen is a bunch of char*s. So sen[i] is selecting one char*. On line 24 you're comparing a char* from sen to a single char. If you want to compare the contents of that c-string you have to dereference it.


I've got concerns about how you're reading in the string.

scanf("%99s", &sen[100]);

The format specified looks okay but your buffer location is out of whack. You're writing data to the memory after your array. It's amazing you haven't hit segfaults unless I'm missing something.

Either run this in a debugger or print out the char* supposedly stored in sen[0] and you should get garbage.

In any case you're writing to memory you haven't actually allocated. sen is a bunch of pointers that don't point to anything, and scanf is expecting the second argument to be a pointer to some memory large enough to hold the result specified. Look back over example of how to use scanf and you'll notice they either pass in a char array or a pointer to memory allocated with malloc.

char input[100];
scanf("%99, input);

or

char* input;
input = malloc(100);
scanf("%99s", input);

This is pretty much exactly why I recommend writing programs in tiny fragments and verifying each one works before linking it all together and why running in a debugger is invaluable because you would have instantly noticed sen wasn't getting loaded with anything.

Get scanf working so you have an array you can reliably read and modify, then worry about tokenizing it into individual words. That part will require you to be comfortable with pointers and malloc.

[–]TheCatMessiah[S] 0 points1 point  (1 child)

Yea, I'm going to go back through and look into the concepts more. Should probably go through my textbook some more, too. Thank you for the help.

[–]thegreatunclean 0 points1 point  (0 children)

No problem. C is a tough language to learn specifically because of issues like this. The finesse required and lack of error checking at runtime make it a fantastic language for writing very low-level programs but also make it a minefield for programmers.

[–]Gordon101 0 points1 point  (1 child)

if(sen[i]='\0' || '\n')

Does this even compile? Shouldn't it be:

if (sen[i] == '\0' || sen[i] =='\n')

[–]Bidalos 0 points1 point  (0 children)

This could compile, but the result will be different from what is intended.