Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pointer Access violation

0 views
Skip to first unread message

tfelb

unread,
Jan 8, 2009, 8:07:21 AM1/8/09
to
Hey group!

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

}

}

Ben Bacarisse

unread,
Jan 8, 2009, 8:28:39 AM1/8/09
to
tfelb <tomi...@gmail.com> writes:

> 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.

tfelb

unread,
Jan 8, 2009, 8:39:36 AM1/8/09
to
On 8 Jan., 14:28, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> Ben.- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Thanks!

char *test[] = { "test", "test" };
chopArray(test);

strlen(*p) refers to 4. That's true because each element of test is
4 !?

CBFalconer

unread,
Jan 8, 2009, 6:41:29 PM1/8/09
to
tfelb wrote:
>
> 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.

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.

tfelb

unread,
Jan 9, 2009, 5:06:03 AM1/9/09
to

Thank you! I fixed that problem. The error was in the initialization
process.

0 new messages