Returning pointer to array problem II

C

Carramba

hi!

the code is cinpiling with gcc -ansi -pedantic. 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*/
*pInt = search_char( &cStr , &cSearch );
for(i=0;i<20;i++){
printf("%d\n",&pInt[0]);
}
return 0;
}

int search_char( char *pStr , char *pSearch ) {
int i;
int vPossition[201];
for( i=0;i<=200;i++){
if(pStr == pSearch[0]){
printf("Found %c in position %d \n", pStr,i+1);
vPossition = i;/*position into array*/
}
}
return vPossition;
}

--

Thanx in advance
________________________
BTW. I know my english is not best in the word, so please stop bugging me
about my speling. And yes Iam sorry you don't understand what I mean, but
there is no point to yell at me. Have a nice day.
 
J

Jens.Toerring

Carramba said:
the code is cinpiling with gcc -ansi -pedantic.

You better also use '-W' and '-Wall'...
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 ] );

Note that "pInt" 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?
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 is '\0'? Otherwise you're rather likely to
comparing elements of pStr that never got assigned a value.
if(pStr == pSearch[0]){
printf("Found %c in position %d \n", pStr,i+1);
vPossition = i;/*position into array*/


Now, 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
 
P

pete

Carramba said:
hi!

the code is cinpiling with gcc -ansi -pedantic.
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.

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

#define STRING_LENGTH 200
#define SEARCH_LENGTH 20
#define str(x) # x
#define xstr(x) str(x)

int main(void) {
char cStr[STRING_LENGTH];
char cSearch[SEARCH_LENGTH];
int rc;
char *ptr;

puts("Enter a string or enter an empty string to quit.");
rc = scanf("%" xstr(STRING_LENGTH) "[^\n]%*[^\n]", cStr);
if (!feof(stdin)) {
getchar();
}
while (rc == 1) {
puts("Enter search string or enter an empty string to
quit.");
rc = scanf("%" xstr(SEARCH_LENGTH) "[^\n]%*[^\n]", cSearch);
if (!feof(stdin)) {
getchar();
}
if (rc != 1) {
break;
}
ptr = strstr(cStr, cSearch);
if (ptr != NULL) {
puts("Search string found.");
puts(ptr);
} else {
puts("Search string not found.");
}
puts("Enter a string or enter an empty string to quit.");
rc = scanf("%" xstr(STRING_LENGTH) "[^\n]%*[^\n]", cStr);
if (!feof(stdin)) {
getchar();
}
}
return 0;
}

/* END new.c */
 
C

Carramba

thanx ! you answer realy helpt! and I got it working!

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
Note that "pInt" 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.

the reason of only 20 is for testing purposes, it was enought to se if
prog. was runing corect
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 is '\0'? Otherwise you're rather likely to
comparing elements of pStr that never got assigned a value.
if(pStr == pSearch[0]){
printf("Found %c in position %d \n", pStr,i+1);
vPossition = i;/*position into array*/

and you are right again!
Now, 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.


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!).



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
well, I have wrote that after some people on group have buggt me to much
about it! I'am trying my best


--

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top