473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Returning pointer to array problem

hi!

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?

so far everything works fine.

int search_char( char *pStr , char *pSearch );

int main(void){
char cStr[201];
char cSearch[21];
int *pInt;
int i ;
printf("Enter string\n",lillae);
scanf("%s", cStr);
printf("Enter char to sertch\n");
scanf("%s", cSearch);
*pInt = search_char( &cStr , &cSearch );
printf("Main prints here:\n");
for(i=0;i<20;i++){ //<== here problem starts
printf("%d\n",&pInt[i]);
return 0;
}

int search_char( char *pStr , char *pSearch ) {
printf("Funktion prints here:");
int i;
int vPossition[201];
for( i=0;i<=200;i++){
if(pStr[i] == pSearch[0]){
printf("Found %c in possition %d \n", pStr[i],i+1);
vPossition[i] = i; //<== here problem starts ????
}
}
return vPossition;//<== here problem starts ???
}

--

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
4 4249
Carramba wrote:
hi!

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?

so far everything works fine.

int search_char( char *pStr , char *pSearch );

int main(void){
char cStr[201];
char cSearch[21];
int *pInt;
int i ;
printf("Enter string\n",lillae);
scanf("%s", cStr);
printf("Enter char to sertch\n");
scanf("%s", cSearch);
*pInt = search_char( &cStr , &cSearch );
printf("Main prints here:\n");
for(i=0;i<20;i++){ //<== here problem starts
printf("%d\n",&pInt[i]);
return 0;
}
Your code is poorly indented and the main function lacks the closing
brace, so the program won't compile.
int search_char( char *pStr , char *pSearch ) {
printf("Funktion prints here:");
int i;
Mixing statements and declarations of local variables is not allowed in
standard C.
int vPossition[201];
for( i=0;i<=200;i++){
if(pStr[i] == pSearch[0]){
printf("Found %c in possition %d \n", pStr[i],i+1);
vPossition[i] = i; //<== here problem starts ????
}
}
return vPossition;//<== here problem starts ???
}

-- August

(If your native tongue is not a western language I will be indulgent.)
Nov 14 '05 #2
Your code is poorly indented and the main function lacks the closing
brace, so the program won't compile.


it's only copy and paste problem... so code ll compile

int search_char( char *pStr , char *pSearch ) {
printf("Funktion prints here:");
int i;


Mixing statements and declarations of local variables is not allowed in
standard C.


don't realy understand what you mean... were do I do that?
int vPossition[201];
for( i=0;i<=200;i++){
if(pStr[i] == pSearch[0]){
printf("Found %c in possition %d \n", pStr[i],i+1);
vPossition[i] = i; //<== here problem starts ????
}
}
return vPossition;//<== here problem starts ???
}

-- August

(If your native tongue is not a western language I will be indulgent.)


--

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 #3
Carramba wrote:
Your code is poorly indented and the main function lacks the closing
brace, so the program won't compile.

it's only copy and paste problem... so code ll compile

int search_char( char *pStr , char *pSearch ) {
printf("Funktion prints here:");
int i;

Mixing statements and declarations of local variables is not allowed
in standard C.

don't realy understand what you mean... were do I do that?


Just before my comment. The `printf' statement comes before the `int i'
declaration. All local variables must be declared before any statements
in the function body.

For instance, you haven't included stdio.h and you haven't declared the
variable `lillae' in the `main' function so the program still won't
compile. What compiler are you using??? Come back when you have
something that compiles.
-- August
Nov 14 '05 #4
August Karlstrom <fu********@comhem.se> writes:
Carramba wrote:

[...]
int search_char( char *pStr , char *pSearch ) {
printf("Funktion prints here:");
int i;


Mixing statements and declarations of local variables is not allowed
in standard C.


It is allowed in C99. (It's also allowed in C++, which is relevant
only if you're using a C++ compiler to compile your C code, which is
usually a bad idea.)

It's also allowed as an extension by some compilers; for example,
"gcc -ansi" accepts it, but "gcc -ansi -pedantic" issues a warning.

But if you care about portability to pre-C99 implementations, you
should avoid using this feature.

And August is correct, the code you posted cannot be compiled. We
*might* be able to guess which errors are in your original code and
which are the result of cut-and-paste errors, but it should be much
easier for you to post your actual code than for us to play guessing
games. Proper indentation will make it much easier to read, and make
it much more likely that you'll get answers.

It may seem like we're being overly picky, but we're really not.
We're trying to help you to help us to help you.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5

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

Similar topics

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...
11
by: Justin Naidl | last post by:
class Foo { protected: char foo_stuff; public: char* get_foo_stuff(); } Given the above example. What I want to know is the "proper/standard" way
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...
3
by: Carramba | last post by:
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...
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: ...
18
by: svata | last post by:
Hello to all, as a result from my previous post I'm busy with splitting code into functions. The one problem ( out of many ) I encounter is how to properly use/code a function which returns...
3
by: Peter Oliphant | last post by:
Below are the definitions of two classes. VList creates two static integer arrays (v_0, v_1), creates an array of pointers to these arrays (vlist), and has a public method to return a pointer to...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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...

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.