tags:

views:

153

answers:

6

hello, i'm trying to populate the heap with a string array, but the console gave me nothing when i compiled. I don't know what I did wrong...

void spellCheck(char article[], char dictionary[]) {
    int i = 0;
    char* tempArticle;
    while ( article[i] != '\0'){
     i++;
    }
    tempArticle = malloc(i);
    i=0;
    while (article[i] != '\0'){
     strcpy(tempArticle, article[i]);
    }
    printf("%s", tempArticle);
}
+4  A: 

You can use strlen for the string length. Also, you are using strcpy a lot of times. Ultimately, you are copying only the null terminator. The function strcpy does this work for you. strcpy copies each character up to an expected null terminator into your new buffer. In other words, you don't use it to copy a character at a time but a string at a time. Although, I'm not sure what your function is intended to do at this point.

void spellCheck(char article[], char dictionary[]) {
    int i = 0;
    char* tempArticle;

    /*while ( article[i] != '\0'){
     i++;
     }*/

    tempArticle = malloc((strlen(article)+1) * sizeof(char));
    /*i=0;
    while (article[i] != '\0'){
     strcpy(tempArticle, article[i]);
    }*/

     strcpy(tempArticle, article);
    printf("%s", tempArticle);
}

Again, in other words, your second loop was essentially trying to copy a smaller and smaller string into the tempArticle buffer until you ended up copying an empty string into it.

(minor point, another answerer pointed out that your second loop was an infinite loop because i was not being incremented. However, you still the had the other problems mentioned)

Edit

So to answer the comment of converting it to lowercase. In general to convert an entire string to lowercase, you would just do something like this:

char s[] = "THE String.";
char *c;
for(c = s; *c != '\0'; c++)
    *c = (char)tolower(*c);
BobbyShaftoe
thank you bobby, what if I want to perform an operation on article (say... lowerCase every word) then string copy to tempArticle, what should I do in that case?
Laurence Gonsalves
@Laurance Gonsalves, yeah I noticed that. There were a few issues. I tried to address what it seemed like he wanted to do.
BobbyShaftoe
@metashockwave - You check to see if the *c == ' ' in the loop above, and if it is you set a flag. If it is not ' ', then you use the flag to decide whether to call tolower(unset) or toupper(set). If you called toupper, unset the flag. Finally, call strcpy on the finished string to move it into tempArticle.
Tom Leys
+1  A: 

strcpy copies an entire string, not simply a character. http://cplusplus.com/reference/clibrary/cstring/strcpy/

Also, strlen() returns the length of a string. You could make your code much shorter and readable by having it be something like this:

void spellCheck(char article[], char dictionary[])
{
    char *tempArticle = malloc(sizeof(char)*(strlen(article)+1));
    strcpy(tempArticle, article);
    // deal with tempArticle
    free(tempArticle)
}

the +1 in the malloc line is so that the end-of-line character gets a space.

Litherum
This is good. The only thing is you don't (or shouldn't, really) need to cast the return value of malloc. (This is from a very old problem that modern standard C compilers don't have anymore).
BobbyShaftoe
thank you litherum, but im not sure what does (char*) before "malloc" do...
It's a C-style cast, converting "void*" to "char*".
Kevin
It should also be noted that in C (not in C++), casting a `void *` to another pointer type is implicit and should be avoided (because if you accidentally use a cast for a function that returns a non-pointer type, you will cover up a subtle bug).
Chris Lutz
+1  A: 

This

i=0;
while (article[i] != '\0') {
  strcpy(tempArticle, article[i]);
}

is an infinite cycle (unless article[0] happens to be 0). You need to fix that for sure. Although there are quite a few things that need to be fixed in that code, but this is a good start.

AndreyT
Good catch, I didn't notice he wasn't advancing i. But at any rate, it's not the right thing anyway.
BobbyShaftoe
A: 

Hello, you are not increasing i on:

while (article[i] != '\0'){ strcpy(tempArticle, article[i]); }

Koder_
A: 

The problem is in here:

i = 0;
while (article[i] != '\0') {
  strcpy(tempArticle, article[i]);
}

Strcpy copies a whole string, and you forgot to increment i, and the strcpy expect the second argument to be a char pointer (not a char). So you are copying garbage into tempArticle over and over again.

Try this:

for (int i=0; article[i] != '\0'; ++i) {
  tempArticle[i] = article[i];
}

There are other design issues in your code, but this is probably the only one preventing things from working.

cdiggins
+1  A: 

A lot of people seem to think you're trying to copy a single string. The subject and your intro doesn't seem like that's the case. Are you sure you're not trying to do something like this?

void spellCheck(char *article[], char dictionary[]) {
    int i = 0;
    char** tempArticle;
    while (article[i] != NULL) {
        i++;
    }
    tempArticle = malloc(i*sizeof(char*));
    i=0;
    while (article[i] != NULL) {
        tempArticle[i] = strdup(article[i]);
        i++;
    }
}
asveikau
I don't think he wants to copy a string array. I think he meant a char array. He's printing the new string at the end, he's not trying to print a string array.
Kevin
Sure. Based on his code I found it pretty confusing what he was actually _trying_ to do, so I thought I'd throw it out there in case it's helpful.
asveikau
+1 nice lateral thought there.
Tom Leys