I want to create a function to remove the last character in each array
element (its an array of char pointers) but I got an access violation.
I'm new to pointer to pointers, I have no idea.
Thanks for any help!
Tom
Here is my function:
void remArray(char *s[])
{
char **p = s;
int i = 0;
while(*p)
{
p[i++][strlen(*p)-1] = '\0'; // terminate the last character
in each array element
p++; //move to next array element
}
}
> I want to create a function to remove the last character in each array
> element (its an array of char pointers) but I got an access violation.
> I'm new to pointer to pointers, I have no idea.
<snip>
>
> void remArray(char *s[])
> {
> char **p = s;
>
> int i = 0;
>
> while(*p)
> {
> p[i++][strlen(*p)-1] = '\0'; // terminate the last character
> in each array element
Your main problem is that you use both p++ and p[i++] to access the
array. Pick one or the other. As both increase p[i] does not access
consecutive elements and strlen(*p) does not refer to the string
pointed to by p[i].
Another problem is that str[strlen(str) - 1] = '\0'; does not do what
you think (just try with an example and it should be clear).
> p++; //move to next array element
> }
> }
--
Ben.
Thanks!
char *test[] = { "test", "test" };
chopArray(test);
strlen(*p) refers to 4. That's true because each element of test is
4 !?
Show the rest of the code, especially the portion that sets up the
array in the first place. I.e. both declaration and
initialization. I suspect you are using arrays of non-writable
strings.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Thank you! I fixed that problem. The error was in the initialization
process.