473,464 Members | 1,499 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Returning pointer to array problem II

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[i] == pSearch[0]){
printf("Found %c in position %d \n", pStr[i],i+1);
vPossition[i] = 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.

Nov 14 '05 #1
3 1830
Carramba <no****@note.com> wrote:
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[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?
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*/
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
Carramba wrote:

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 */
--
pete
Nov 14 '05 #3
thanx ! you answer realy helpt! and I got it working!

On Fri, 03 Jun 2005 13:18:27 +0200, <Je***********@physik.fu-berlin.de>
wrote:
Carramba <no****@note.com> 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
Note 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. the reason of only 20 is for testing purposes, it was enought to se if
prog. was runing corect
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!
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

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.

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
9
by: josh | last post by:
Hi, I'm converting (for learning purpose) )a program from Java to C++ and I've a doubt: In Java every argument (variable and reference types) is passed and returned in functions by-value so if I...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.