Heya,
trying to figure out pointers, can't get the following simple app work:
all it should do is print the lower letters of a given string
1) why won't it work? :/Code:#include <stdio.h> #include <stdlib.h> char* selectLower(char* s); int main() { char string[]="This is a TestString"; printf("%s", selectLower(string)); return(0); } char* selectLower(char* s) { char* ptrA=s; char* ptrB=malloc(sizeof(char*)); while (*ptrA!='\0') { if (*ptrA >= 'a' && *ptrA <= 'z') { *(ptrB++)=*(ptrA++); } else { ptrA++; } } *ptrB='\0'; return ptrB; }
2) is the use of malloc correct at all? I want ptrB to only have the size it really needs (meaning only lower letters). Or do I have to count the amount of lower characters first, store it in some int var and then assign the length via malloc(length)?
3) can I somehow prevent that my original string s is being overwritten?
thanks in advance