pointer Access violation

T

tfelb

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

}

}
 
B

Ben Bacarisse

tfelb said:
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.
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 does not access
consecutive elements and strlen(*p) does not refer to the string
pointed to by p.

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

tfelb

tfelb said:
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.
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 does not access
consecutive elements and strlen(*p) does not refer to the string
pointed to by p.

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


Thanks!

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

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

CBFalconer

tfelb said:
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.
 
T

tfelb

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.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top