patterncMinor
Modifying a string by passing a pointer to a void function
Viewed 0 times
passingfunctionvoidpointermodifyingstring
Problem
Please explain the relationship between pointers and arrays. In this tutorial, they change
Also, please review my code for reversing a string. The string variable belongs to the
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
#include
#include
void strreverse(char *string)
{
int i, len;
char c;
len=strlen(string);
char string2[len+1];
for(i=0; i<len; i++)
{
c=string[i];
string2[len-i]=c;
}
string2[len+1]='\0';
string=string2;
//printf("%s\n", string); <-- 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(&str[0]);
printf("%s", str);
return EXIT_SUCCESS;
}Solution
No, I've compiled your code with C90 and it doesn't reverse the given string.
To understand pointer-array relationship read wikipedia.
Array names are not really pointers but can be used as pointers. Instead of
You've passed argument as pointer but your code still fails, why?
You should have done this;
Now that you're done reversing, you need to understand the implication of your next move.
but this will only alter the first value of the array since
Now your string is reversed.
Read this wikipedia article to understand how an array can be reversed faster.
int main(int argc, char *argv[])If you're not compiling from the command line with arguments to be used by the program, thenint main()suffices.
- Passing argument by pointer is used when you want the value of the variable changed. Say I have a variable
int varand a functionchange(.), I want change to alter the value ofvar. If I declare change asvoid change(int n)and I callchange(var), functionchangewill take a copy ofvarnamedvarbut it's only a copy, its address is different from the originalvar. Any change you make on copyvarwill not affect the originalvar. If i declare change asvoid change(int* n), then change will only acceptintpointers, I have to pass the address,&varintochangeaschange(&var). Now working on this address is exactly like working on the initialvar.
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 commutativeArray names are not really pointers but can be used as pointers. Instead of
strreverse(&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
fgetsis 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,fgetsexits when the length argument is reached or it encounters a newline. In summary,fgetsreads 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 variablestrbecomes"my string\n".
- So you got the length of the string into
len. The arraystringis zero-based, callingstring[len]return thecharafter the desired last one. The last char isstring[len - 1].
You should have done this;
char string2[len];
for(i=0; i<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(&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<=len; i++)
string[i] = string2[i];Now your string is reversed.
Read this wikipedia article to understand how an array can be reversed faster.
Code Snippets
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 commutativechar string2[len];
for(i=0; i<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';string = string2;*string = *string2;for(i=0; i<=len; i++)
string[i] = string2[i];Context
StackExchange Code Review Q#31056, answer score: 3
Revisions (0)
No revisions yet.