Re: Returning pointer to array problem II
- From: Carramba <nospam@xxxxxxxx>
- Date: Sat, 04 Jun 2005 11:40:53 +0200
thanx ! you answer realy helpt! and I got it working!
On Fri, 03 Jun 2005 13:18:27 +0200, <Jens.Toerring@xxxxxxxxxxxxxxxxxxx> wrote:
Carramba <nospam@xxxxxxxx> wrote:the code is cinpiling with gcc -ansi -pedantic.
You better also use '-W' and '-Wall'...
I ll do it, and se if it's makes me writte better code :)
so Iam back to my question Iam trying to make program were I enter string and serach char. and funktion prints out witch position char is found this is done if funktion serach_char. so far all good what I want do next is: return, from funktion, pointer value to array were positions ( of found char) is stored. and print that array from main. but I only manage to print memory adress to array..
any suggestions?
#include <stdio.h> int search_char( char *pStr , char *pSearch );
int main(void){
char cStr[201];
char cSearch[21];
int *pInt;
int i ;
printf("Enter string\n");
scanf("%s", cStr);
printf("Enter search char\n");
scanf("%s", cSearch); /*reading it as string becouse I want to
by able later search 2 chars and more*/
I hope you understand that this use of scanf() is dangerous - if the user inputs more non-white-space characters than the char arrays are long then your program writes past the end of these arrays...
*pInt = search_char( &cStr , &cSearch );
If search_char() would return an int pointer (as it probably should) then this would have to be written as
pInt = search_char( cStr, cSearch );
Note that all the '*'s and '&'s you are using don't make sense. On the keft hand side you have a pointer and you want to assign the return value to that pointer, not to what it's pointing to. On the right hand side both arrays are already passed by pointer to the first element without the '&' - with the ampersands you would pass pointers to the arrays, not pointers to the first elements. Whil the addresses passed to the function would be the same, they are different types and the compiler should warn you about that if you adjust the warning level to something reasonable.
for(i=0;i<20;i++){ printf("%d\n",&pInt[0]);
And this probably should be
printf( "%d\n", pInt[ i ] );
yes, my misstake
the reason of only 20 is for testing purposes, it was enought to se if prog. was runing corectNote that "pInt[i]" is absolutely identical to "*(pInt+i)" (that's what the compiler is going to convert it to). But then the array you try (unsuccessfully, see below) to return from search_char() can have up to 201 elements, so why only print 20 of them? You don't even know how many of them got set.
return 0; }
int search_char( char *pStr , char *pSearch ) {
Why do you declare this function to return an int when you actually try to make it return an int pointer?
I did'n realise that! but thanx for pointing it out!
int i; int vPossition[201]; for( i=0;i<=200;i++){
Shouldn't you rather stop when you reach the end of the string, i.e. when pStr[i] is '\0'? Otherwise you're rather likely to comparing elements of pStr that never got assigned a value.
if(pStr[i] == pSearch[0]){ printf("Found %c in position %d \n", pStr[i],i+1); vPossition[i] = i;/*position into array*/
and you are right again!
well, I have wrote that after some people on group have buggt me to much about it! I'am trying my bestNow, that's probably not what you really want - you only set those elements of the vPossitions array where there's a match and you leave the rest of them in some random state. What would that be good for? You can't figure out afterwards which have been set and which haven't.
} } return vPossition;
And this is a plain mistake. You try to return a variable (or array) local to this function. The moment this function is left, this aray will stop to existand the calling function can't do nothing at all with the return value. So here you throw away all the information the function assembled. If you want to return the array then you must use one that doesn't vanish once the function is left. You can do that by either making the array a static array (but note that each new invocation of the function will overwrite the results from the pre- vious invocation) or by returning a pointer to some memory you allo- cated in this function (but then the calling function must take care of deallocation!).
}
BTW. I know my english is not best in the word, so please stop bugging me
about my speling.
Perhaps not too clever an attitude when you expect answers from other people - spelling can sometimes be rather important to make ones mea- ning clear and bad spelling definitely makes your questions harder to read. Do you realize that "speling" gets spelt with two 'l's? ,-)
Regards, Jens
--
Thanx in advance
________________________
BTW. I know my english is not best in the word, so please stop bugging me about my spelling. And yes Iam sorry you don't understand what I mean, but there is no point to yell at me. Have a nice day.
.
- References:
- Returning pointer to array problem II
- From: Carramba
- Re: Returning pointer to array problem II
- From: Jens . Toerring
- Returning pointer to array problem II
- Prev by Date: Re: Why compiler not generating any warning ?
- Next by Date: Re: Why compiler not generating any warning ?
- Previous by thread: Re: Returning pointer to array problem II
- Next by thread: Re: Returning pointer to array problem II
- Index(es):
Relevant Pages
|