0

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.

3
  • tagging this question "homework" Commented Sep 7, 2011 at 4:04
  • Let's see if I can clarify I will update my question. Commented 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. Commented Sep 7, 2011 at 4:58

4 Answers 4

3

Pass it as a double pointer:

type myFunc(char **array) {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Normally, someone talking about an array of C strings would be talking about an array of pointers to several different arrays-of-char.
0

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

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*"
0
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])
{
}

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.