Modifying a string by passing a pointer to a void function Please explain the relationship between pointers and arrays.  In this tutorial, they change int c by changing *r in the function.  How is that possible?

Also, please review my code for reversing a string.  The string variable belongs to the main() function, and I would like to pass a pointer to let the strreverse() function write to the string.  Does my code work?

#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;string.h&gt;

void strreverse(char *string)
{
 int i, len;
 char c;
 len=strlen(string);
 char string2[len+1];

 for(i=0; i&lt;len; i++)
 {
  c=string[i];
  string2[len-i]=c;

 }
 string2[len+1]='\0';
 string=string2;
 //printf("%s\n", string); &lt;-- this would work,but should be in main

}

int main(int argc, char *argv[])
{
  char str[256];
  printf("Type a String to reverse it.[max. 255 chars]\n");
  fgets(str, 255, stdin);
  strreverse(&amp;str[0]);
  printf("%s", str);
  return EXIT_SUCCESS;
}
No, I've compiled your code with C90 and it doesn't reverse the given string.


int main(int argc, char *argv[]) If you're not compiling from the command line with arguments to be used by the program, then int main() suffices.
Passing argument by pointer is used when you want the value of the variable changed. Say I have a variable int var and a function change(.), I want change to alter the value of var. If I declare change as void change(int n) and I call change(var), function change will take a copy of var named var but it's only a copy, its address is different from the original var. Any change you make on copy var will not affect the original var. If i declare change as void change(int* n), then change will only accept int pointers, I have to pass the address, &amp;var into change as change(&amp;var). Now working on this address is exactly like working on the initial var.


To understand pointer-array relationship read wikipedia.

int array[5];      // Declares 5 contiguous integers 
int *ptr = array;  // Arrays can be used as pointers 
ptr[0] = 1;        // Pointers can be indexed with array syntax 
*(array + 1) = 2;  // Arrays can be dereferenced with pointer syntax 
*(1 + array) = 3;  // Pointer addition is commutative 
2[array] = 4;      // Subscript operator is commutative 


Array names are not really pointers but can be used as pointers. Instead of  strreverse(&amp;str[0]), you can do strreverse(str). Same result.

You've passed argument as pointer but your code still fails, why?


One thing to know about fgets is that unless there is an [EOF][2] which you can only get from an input file or Ctrl+Z if you run from the command line, fgets exits when the length argument is reached or it encounters a newline. In summary, fgets reads the newline as a character when you press enter key, increasing your desired length by 1. So if I had entered "my string" + Enter, your variable str becomes "my string\n".
So you got the length of the string into len. The array string is zero-based, calling string[len] return the char after the desired last one. The last char is string[len - 1]. 


You should have done this;

char string2[len];

for(i=0; i&lt;len; i++){
  c=string[i];      //variable c is unimportant
  string2[len-i-1] = string[i];  //observe the -1, when i = 0, string2[len - 1] = string[0]
}
string2[len] ='\0';


Now that you're done reversing, you need to understand the implication of your next move.

string = string2;


string is a pointer, but that doesn't make it any less of a variable, it's a pointer variable, it also has an address. And if I declare a pointer variable to its address, that pointer will also have an address. Going back to what I said earlier, when you call change(&amp;var), a copy of the address of var is passed into the function, so when you change the value of this pointer, it no longer holds the address of var. You may think about dereferencing, like this 

*string = *string2;


but this will only alter the first value of the array since *string is same as string[0]. The solution is to copy element by element.

for(i=0; i&lt;=len; i++)
  string[i] = string2[i];


Now your string is reversed.

Read this wikipedia article to understand how an array can be reversed faster.