Storing in array

Discussion in 'C Programming' started by Kay, Oct 4, 2004.

  1. Kay

    Kay Guest

    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 = "\n";

    While( !feof(ptr)){

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

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

    if( array != port && array == "\n" ){
    strcpy( array, port );
    }
    }
     
    Kay, Oct 4, 2004
    #1
    1. Advertising

  2. Kay wrote:
    > 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?
    >


    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.

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


    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 ?


    > array = "\n";


    Why ? Even if you had meant to assign '\n' character
    to all the contents it is still wrong.
    You have to use strcpy.

    >
    > While( !feof(ptr)){


    You had posted a code fragment here.
    You have to open a file, in the appropriate mode
    before you could do this.

    >
    > fscanf(fptr, "%s", port );
    >
    > for(int i = 0; i < 7; i++ ){
    >
    > if( array != port && array == "\n" ){
    > strcpy( array, port );
    > }
    > }
    >


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

    --
    Karthik.
    http://akktech.blogspot.com .
     
    Karthik Kumar, Oct 5, 2004
    #2
    1. Advertising

  3. Karthik Kumar wrote:
    > Kay wrote:
    >
    >>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?
    >>

    >
    >
    > 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.
    >
    >
    >>for( int i = 0; i < 7; i++)


    Try to use size_t for array indices instead of
    int. It fits better.

    >
    >> array = "\n";

    >


    --
    Karthik.
    http://akktech.blogspot.com .
     
    Karthik Kumar, Oct 5, 2004
    #3
  4. Kay

    -berlin.de Guest

    Kay <> wrote:
    > 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?


    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.

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


    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?

    > While( !feof(ptr)){


    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.

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


    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.

    > for(int i = 0; i < 7; i++ ){
    >
    > if( array != port && array == "\n" ){


    Now, why should 'port' and array 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 and 'port'
    but not the strings they are pointing to. There's strcmp() for
    that.

    > strcpy( array, port );


    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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Oct 5, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hale
    Replies:
    1
    Views:
    5,107
    Kemper
    Dec 28, 2003
  2. =?Utf-8?B?QnJhZA==?=

    Storing an array to a Session

    =?Utf-8?B?QnJhZA==?=, Dec 22, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    555
    =?Utf-8?B?QnJhZA==?=
    Dec 22, 2005
  3. =?Utf-8?B?dGhlIGZyaWVuZGx5IGRpc3BsYXkgbmFtZQ==?=

    Problems storing a byte array

    =?Utf-8?B?dGhlIGZyaWVuZGx5IGRpc3BsYXkgbmFtZQ==?=, Jan 2, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    466
    =?Utf-8?B?dGhlIGZyaWVuZGx5IGRpc3BsYXkgbmFtZQ==?=
    Jan 2, 2006
  4. toton
    Replies:
    11
    Views:
    714
    toton
    Oct 13, 2006
  5. Jonathan Wood
    Replies:
    1
    Views:
    511
    Jonathan Wood
    Jun 2, 2008
Loading...

Share This Page