Returning pointer to array problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carramba

    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",lilla e);
    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("Funktio n 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.

  • August Karlstrom

    #2
    Re: Returning pointer to array problem

    Carramba wrote:[color=blue]
    > 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",lilla e);
    > 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;
    > }[/color]

    Your code is poorly indented and the main function lacks the closing
    brace, so the program won't compile.
    [color=blue]
    > int search_char( char *pStr , char *pSearch ) {
    > printf("Funktio n prints here:");
    > int i;[/color]

    Mixing statements and declarations of local variables is not allowed in
    standard C.
    [color=blue]
    > 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 ???
    > }
    >[/color]


    -- August

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

    Comment

    • Carramba

      #3
      Re: Returning pointer to array problem

      [color=blue]
      > Your code is poorly indented and the main function lacks the closing
      > brace, so the program won't compile.[/color]

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

      [color=blue][color=green]
      >> int search_char( char *pStr , char *pSearch ) {
      >> printf("Funktio n prints here:");
      >> int i;[/color]
      >
      > Mixing statements and declarations of local variables is not allowed in
      > standard C.[/color]

      don't realy understand what you mean... were do I do that?
      [color=blue][color=green]
      >> 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 ???
      >> }
      >>[/color]
      >
      >
      > -- August
      >
      > (If your native tongue is not a western language I will be indulgent.)[/color]



      --

      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.

      Comment

      • August Karlstrom

        #4
        Re: Returning pointer to array problem

        Carramba wrote:[color=blue]
        >[color=green]
        >> Your code is poorly indented and the main function lacks the closing
        >> brace, so the program won't compile.[/color]
        >
        >
        > it's only copy and paste problem... so code ll compile
        >
        >[color=green][color=darkred]
        >>> int search_char( char *pStr , char *pSearch ) {
        >>> printf("Funktio n prints here:");
        >>> int i;[/color]
        >>
        >>
        >> Mixing statements and declarations of local variables is not allowed
        >> in standard C.[/color]
        >
        >
        > don't realy understand what you mean... were do I do that?[/color]

        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

        Comment

        • Keith Thompson

          #5
          Re: Returning pointer to array problem

          August Karlstrom <fusionfive@com hem.se> writes:[color=blue]
          > Carramba wrote:[/color]
          [...][color=blue][color=green]
          >> int search_char( char *pStr , char *pSearch ) {
          >> printf("Funktio n prints here:");
          >> int i;[/color]
          >
          > Mixing statements and declarations of local variables is not allowed
          > in standard C.[/color]

          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_Keit h) kst-u@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.

          Comment

          Working...