Normally I would just pass an array into a function, however, I am doing a homework assignment and my professor says to use an array of C-Strings and I want to use a function. However, I can not think of how to send this to a function. Any help would be great.
-
tagging this question "homework"phoxis– phoxis2011-09-07 04:04:56 +00:00Commented Sep 7, 2011 at 4:04
-
Let's see if I can clarify I will update my question.Travis Pessetto– Travis Pessetto2011-09-07 04:06:47 +00:00Commented Sep 7, 2011 at 4:06
-
Thanks everyone, I appreciate the answers but I realized that there may be one detail I left out which I will work on tomorrow and maybe get help from my schools tutor lab.Travis Pessetto– Travis Pessetto2011-09-07 04:58:42 +00:00Commented Sep 7, 2011 at 4:58
Add a comment
|
4 Answers
Well, since a c-string is really just an array of chars, wouldn't you just pass an array of arrays of chars?
1 Comment
Michael Burr
Normally, someone talking about an array of C strings would be talking about an array of pointers to several different arrays-of-char.
Well, do you know the syntax for an array? Do you know the syntax for a function? How would you guess an array would be passed as an argument?
If you're not sure, take a look in your C/C++ textbook.
1 Comment
Travis Pessetto
I have read the text this whole day...I made this function
void sortArray(char uniqueWords[],int numUniqueWords, char newWord[]), however when I want to run strcmp(uniqueWords[count],newWord) Visual Studio says "Error: argument of type char is incompatible with type constant char*"char **arr;
arr = malloc (sizeof (char *) * string_count);
for (i=0; i<string_count; i++)
{
/* get string size */
arr[i] = malloc (sizeof (char) * this_string_size);
/* copy string into allocated */
}
function (char **arr, int string_count)
{
}
Remember to free memory after use.
OR
char *arr[NOS_OF_SRINGS];
for (i=0; i<NOS_OF_STRINGS; i++)
arr[i] = malloc (sizeof (char) * string_length);
function (char *arr[NOS_OF_STRINGS])
{
}
Remember to free the allocated memory blocks.
OR
char arr[NOS_OF_STRINGS][STR_LENGTH];
/* copy stuffs */
function (char arr[NOS_OF_STRINGS][STR_LENGTH])
{
}