user968102
user968102

Reputation: 502

c double pointer giving error that doesn't make sense

int p_entity(char **data){
  char *pch;

  pch = strtok(*data, " \n");
  // printf("%s ", pch);
  pch = strtok(NULL, " \n");
  // (*data) = (*data) + 1;   // 1. this is okay
  //  (*data) = pch;          // 2. but doing this will cause an error
  printf("%c %d \n", *pch, pch); 
  printf("%c %d \n", **data, *data);
}

From the above code I will get the following if I uncomment 1:

g 4927479
e 4927456

I will get the following if I uncomment 2:

g 4927479
g 4927479
      3 [main] main 8172 exception::handle: Exception: STATUS_ACCESS_VIOLATION
    470 [main] main 8172 open_stackdumpfile: Dumping stack trace to main.exe.stackdump

Can anyone explain why I am getting that error? I would think both assignments would be legal and incrementing (*data) would be equivalent to straight assignning the address I want it to be at.

Upvotes: 0

Views: 140

Answers (2)

Santosh
Santosh

Reputation: 419

Answer really depends on how you are handling data before and after calling this function.

if you are allocating memory to *data before calling this function and tying to free it after this function returns, it could crash, as you have changed its value.

Upvotes: 0

Matthieu
Matthieu

Reputation: 16417

Seems like even when you get the error (and uncomment 2), you still get the two printfs... seems like it is crashing much later in your code.

Upvotes: 2

Related Questions