4

I am trying to convert a const char, to a char... here is my code:

bool check(const char* word)
{

char *temp[1][50];

temp = word;

return true;
}

It is a function with a const char passed in. I need to convert that const char to a char. When I run this code now, the compiler throws up this error:

dictionary.c:28:6: error: array type 'char *[1][50]' is not assignable
temp = word;

How do I correctly complete this conversion?

Thanks, Josh

2 Answers 2

6
#include <string.h>
bool check(const char* word)
{
    char temp[51];
    if(strlen(word)>50)
        return false;
    strncpy(temp,word,51);
    // Do seomething with temp here
    return true;
}
Sign up to request clarification or add additional context in comments.

2 Comments

ok, great that works, now I need to implement the next part of the program, which is checking if the word stored in temp is a correctly spelled word in the dictionary... To do this I have a global variable that holds the dictionary. Here is the code that defines it, so you know what I am working with: char *result = NULL; I need to be able to tell if somewhere in "result" there is a string = to temp. It then needs to return true if an identical string is found (meaning the word is spelled correctly) and false otherwise. If you can also help me with this I would greatly appreciate it!
@JoshuaMcDonald you should ask a new question.
1

If you want a non-const version, you'll have to copy the string:

char temp[strlen(word) + 1];
strcpy(temp, word);

Or:

char * temp = strdup(word);

if(!temp)
{
    /* copy failed */

    return false;
}

/* use temp */

free(temp);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.