Storing in array

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

    #1

    Storing in array

    ABC
    ASF
    DFS
    ASS
    ABC <--- Duplicate
    JJK

    I want to store above char in an arry, but I'm not sure I'm right or not
    anyone can guide me or give me some suggestions, Thx?

    for( int i = 0; i < 7; i++)
    array[i] = "\n";

    While( !feof(ptr)){

    fscanf(fptr, "%s", port );

    for(int i = 0; i < 7; i++ ){

    if( array[i] != port && array[i] == "\n" ){
    strcpy( array[i], port );
    }
    }

  • Karthik Kumar

    #2
    Re: Storing in array

    Kay wrote:[color=blue]
    > ABC
    > ASF
    > DFS
    > ASS
    > ABC <--- Duplicate
    > JJK
    >
    > I want to store above char in an arry, but I'm not sure I'm right or not
    > anyone can guide me or give me some suggestions, Thx?
    >[/color]

    Where is the declaration of the variable 'array' ?
    As a matter of style, try to give it a meaningful name
    other than array . There could be too many arrays in your
    program.
    [color=blue]
    > for( int i = 0; i < 7; i++)[/color]

    How do you know this magic number 7 beforehand ?
    Are you making an assumption that the input
    file is always going to contain at least 7 lines ?

    [color=blue]
    > array[i] = "\n";[/color]

    Why ? Even if you had meant to assign '\n' character
    to all the contents it is still wrong.
    You have to use strcpy.
    [color=blue]
    >
    > While( !feof(ptr)){[/color]

    You had posted a code fragment here.
    You have to open a file, in the appropriate mode
    before you could do this.
    [color=blue]
    >
    > fscanf(fptr, "%s", port );
    >
    > for(int i = 0; i < 7; i++ ){
    >
    > if( array[i] != port && array[i] == "\n" ){
    > strcpy( array[i], port );
    > }
    > }
    >[/color]

    Check out strcmp if you want to compare two strings.
    Please post fully compilable code to get better help.

    --
    Karthik.
    http://akktech.blogspot.com .

    Comment

    • Karthik Kumar

      #3
      Re: Storing in array

      Karthik Kumar wrote:[color=blue]
      > Kay wrote:
      >[color=green]
      >>ABC
      >>ASF
      >>DFS
      >>ASS
      >>ABC <--- Duplicate
      >>JJK
      >>
      >>I want to store above char in an arry, but I'm not sure I'm right or not
      >>anyone can guide me or give me some suggestions, Thx?
      >>[/color]
      >
      >
      > Where is the declaration of the variable 'array' ?
      > As a matter of style, try to give it a meaningful name
      > other than array . There could be too many arrays in your
      > program.
      >
      >[color=green]
      >>for( int i = 0; i < 7; i++)[/color][/color]

      Try to use size_t for array indices instead of
      int. It fits better.
      [color=blue]
      >[color=green]
      >> array[i] = "\n";[/color]
      >[/color]

      --
      Karthik.
      http://akktech.blogspot.com .

      Comment

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

        #4
        Re: Storing in array

        Kay <ericjo92003@ya hoo.com.hk> wrote:[color=blue]
        > ABC
        > ASF
        > DFS
        > ASS
        > ABC <--- Duplicate
        > JJK[/color]
        [color=blue]
        > I want to store above char in an arry, but I'm not sure I'm right or not
        > anyone can guide me or give me some suggestions, Thx?[/color]

        All of this is horribly broken. You use 'array' in some places as if
        it would be an array of char pointers and in others as if it would be
        an array of char arrays - at least that's what it looks like if you
        first assign a pointer to an element of array and later use strcpy()
        to copy to one or more of the elements. Since there's no definition
        of 'array' it's impossible to figure out what you meant it to be.
        [color=blue]
        > for( int i = 0; i < 7; i++)
        > array[i] = "\n";[/color]

        Here you assign a pointer to the string literal "\n" to the first 7
        elements of 'array' which thus must be an array of (at least 7) char
        pointers. Or did you meant to do a strcpy() here?
        [color=blue]
        > While( !feof(ptr)){[/color]

        First, "While" must start with a lower case letter. Second, feof()
        can only be used after you already have tried to read from the
        file, typically you use it to find out if a read failed because
        you hit the end of the file. It won't tell you if the next read
        is going to fail because of that condition.
        [color=blue]
        > fscanf(fptr, "%s", port );[/color]

        This is rather dangerous unless you know exactly that there's
        never going to be a longer that's longer than the memory assigned
        to 'port. Note that you can pass a maximum length of the string
        to be read with '%s' top fscanf() - that can help you eliminate
        this potential problem.
        [color=blue]
        > for(int i = 0; i < 7; i++ ){
        >
        > if( array[i] != port && array[i] == "\n" ){[/color]

        Now, why should 'port' and array[i] ever be identical? I guess you
        try to do a string comparison here but that's not how that works.
        What you do here is comparing the pointers array[i] and 'port'
        but not the strings they are pointing to. There's strcmp() for
        that.
        [color=blue]
        > strcpy( array[i], port );[/color]

        Since all elements of array have proviously been set to point to
        the literal string "\n" this strcpy() won't work - you try to
        copy what 'port' points to over that literal string. But, first
        of all, literal strings can't be changed, and second, even if
        you could do that it would also change what all the other elements
        of 'array' point to.

        It looks a bit as if you have to try to figure out more clearly
        for yourself what the difference between an array and a pointer
        is and that they aren't the same. The next time please post a
        complete program (or at least a complete function) because with-
        out knowing how you defined your variables it's impossible to
        say if you got it right or not.

        Regards, Jens
        --
        \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
        \______________ ____________ http://www.toerring.de

        Comment

        Working...