Returning pointer to array problem II

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

    #1

    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.

  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: Returning pointer to array problem II

    Carramba <nospam@note.co m> wrote:[color=blue]
    > the code is cinpiling with gcc -ansi -pedantic.[/color]

    You better also use '-W' and '-Wall'...
    [color=blue]
    > 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..[/color]
    [color=blue]
    > any suggestions?[/color]
    [color=blue]
    > #include <stdio.h>
    > int search_char( char *pStr , char *pSearch );[/color]
    [color=blue]
    > 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*/[/color]

    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...
    [color=blue]
    > *pInt = search_char( &cStr , &cSearch );[/color]

    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.
    [color=blue]
    > for(i=0;i<20;i+ +){
    > printf("%d\n",& pInt[0]);[/color]

    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.
    [color=blue]
    > return 0;
    > }[/color]
    [color=blue]
    > int search_char( char *pStr , char *pSearch ) {[/color]

    Why do you declare this function to return an int when you actually
    try to make it return an int pointer?
    [color=blue]
    > int i;
    > int vPossition[201];
    > for( i=0;i<=200;i++) {[/color]

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

    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.
    [color=blue]
    > }
    > }
    > return vPossition;[/color]

    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!).
    [color=blue]
    > }[/color]
    [color=blue]
    > BTW. I know my english is not best in the word, so please stop bugging me
    > about my speling.[/color]

    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 ___ Jens.Toerring@p hysik.fu-berlin.de
    \______________ ____________ http://www.toerring.de

    Comment

    • pete

      #3
      Re: Returning pointer to array problem II

      Carramba wrote:[color=blue]
      >
      > 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.[/color]

      /* 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_LEN GTH) "[^\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_LEN GTH) "[^\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_LEN GTH) "[^\n]%*[^\n]", cStr);
      if (!feof(stdin)) {
      getchar();
      }
      }
      return 0;
      }

      /* END new.c */


      --
      pete

      Comment

      • Carramba

        #4
        Re: Returning pointer to array problem II

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

        On Fri, 03 Jun 2005 13:18:27 +0200, <Jens.Toerring@ physik.fu-berlin.de>
        wrote:
        [color=blue]
        > Carramba <nospam@note.co m> wrote:[color=green]
        >> the code is cinpiling with gcc -ansi -pedantic.[/color]
        >
        > You better also use '-W' and '-Wall'...[/color]
        I ll do it, and se if it's makes me writte better code :)
        [color=blue][color=green]
        >> 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..[/color]
        >[color=green]
        >> any suggestions?[/color]
        >[color=green]
        >> #include <stdio.h>
        >> int search_char( char *pStr , char *pSearch );[/color]
        >[color=green]
        >> 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*/[/color]
        >
        > 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...
        >[color=green]
        >> *pInt = search_char( &cStr , &cSearch );[/color]
        >
        > 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.
        >[color=green]
        >> for(i=0;i<20;i+ +){
        >> printf("%d\n",& pInt[0]);[/color]
        >
        > And this probably should be
        >
        > printf( "%d\n", pInt[ i ] );[/color]

        yes, my misstake
        [color=blue]
        > 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.[/color]
        the reason of only 20 is for testing purposes, it was enought to se if
        prog. was runing corect[color=blue][color=green]
        >> return 0;
        >> }[/color]
        >[color=green]
        >> int search_char( char *pStr , char *pSearch ) {[/color]
        >
        > Why do you declare this function to return an int when you actually
        > try to make it return an int pointer?[/color]
        I did'n realise that! but thanx for pointing it out!
        [color=blue][color=green]
        >> int i;
        >> int vPossition[201];
        >> for( i=0;i<=200;i++) {[/color]
        >
        > 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.
        >[color=green]
        >> if(pStr[i] == pSearch[0]){
        >> printf("Found %c in position %d \n", pStr[i],i+1);
        >> vPossition[i] = i;/*position into array*/[/color][/color]
        and you are right again!
        [color=blue]
        > 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.
        >[color=green]
        >> }
        >> }
        >> return vPossition;[/color]
        >
        > 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!).
        >[color=green]
        >> }[/color]
        >[color=green]
        >> BTW. I know my english is not best in the word, so please stop bugging
        >> me
        >> about my speling.[/color]
        >
        > 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[/color]
        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.

        Comment

        Working...