CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2006
    Posts
    99

    Angry question about assigning values to a array member.

    hello guys,

    i have a very simple class:

    class A
    {
    float k[100];

    void assignValuesToK()
    {
    assign(k);
    }

    };

    and a simple function outside the class A:

    assign(float *)
    {
    for(int i=0;i<100;i++)
    k[i]=i;
    }


    the value assignment seems to be ok if i check the k[] inside the function assign().

    however, if i want to print the values of the array k[] in a member function of A after i call assignValuesToK(), i saw a bunch of zeros.

    whay? this looks very strange to me.

    thank you.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: question about assigning values to a array member.

    Looks fine, the problem must be elsewhere.

    Although, it would be wise to pass the length of the array to assign() rather than hard-coding it. Makes no difference now, but you should be in the habit of always passing an array length along with an array.

  3. #3
    Join Date
    May 2002
    Posts
    1,435

    Re: question about assigning values to a array member.

    That code is full of errors and will never compile under any circumstances. Please post real code.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: question about assigning values to a array member.

    There are issues with the assign function not having a return type or a parameter name, but I don't see any other syntax errors....?

  5. #5
    Join Date
    May 2002
    Posts
    1,435

    Re: question about assigning values to a array member.

    Lindley, I do agree with you that the code will produce the correct results - if the errors are taken care of. But my issue is with sloppy questions. I simply don't care to answer the same way as you when a question is presented this way.

    If these errors are fixed then the code will compile and produce correct results:

    (1) Members of A are private by default so they cannot be accessed by external function.

    (2) assign(float *) has no return type.

    (3) k[i] = i; (k is undefined)

    I consider code with 3 errors in 13 lines (23%) to be full of errors.

    And I am being generous with my error count since there would actually be more than one per line of code if compiled as presented.

  6. #6
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: question about assigning values to a array member.

    Quote Originally Posted by 0xC0000005 View Post
    That code is full of errors and will never compile under any circumstances. Please post real code.
    The OP claims that he ran the code with the result being an array full of zeroes. Yet there is no code that shows how the k member is initialized. Moreover, the assignment is not assigning the input to the function at all but the i variable used to control the for loop. There are also too many k arrays for me to keep track of. I don't know which k he is talking about at any given time. The k class member isn't initialized but the other k ( no idea what class or namespace this one belongs to) should have the values 0-99. As far as what the print function is doing, I have no idea which k is being printed.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: question about assigning values to a array member.

    Quote Originally Posted by 0xC0000005 View Post
    But my issue is with sloppy questions. I simply don't care to answer the same way as you when a question is presented this way.
    If I didn't answer sloppy questions, I'd ignore 95&#37; of the threads on here. Maybe one in 30 newbies actually bothers to use code tags, and perhaps one in 5 presents enough information the first time to concisely evaluate the desired behavior and the problem.

    Fortunately most at least know English, although a fair number seem to have a tenuous grasp of grammar at best.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: question about assigning values to a array member.

    Quote Originally Posted by Lindley View Post
    If I didn't answer sloppy questions, I'd ignore 95% of the threads on here.
    After 88 posts, you would think the OP would know to use code tags and also have some inkling of posting valid code.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: question about assigning values to a array member.

    Quote Originally Posted by billconan View Post
    assign(float *)
    {
    for(int i=0;i<100;i++)
    k[i]=i;
    }
    Even if you had given a return value and specified k as the parameter, this non-member function isn't the brightest idea. If I saw something like this in production code, I would have serious word with the developer. The function will take any float pointer, and assumes that there are 100 elements - this is very dangerous! If you are not going to make assign a private member of A (and make it a function that takes no parameters), then please do one of the following (or something else sensible):

    1) Change the argument so that it passes in a reference to a float stack array that holds 100 elements:
    Code:
    void assign(float(&arr)[100])
    {
      for(size_t i=0; i<100; i++)
      {
        arr[i] = float(i);
      }
    }
    or

    2) Change the argument so that it passes in a reference to a float stack array that holds an arbitrary number of elements:
    Code:
    template<size_t size>
    void assign(float(&arr)[size])
    {
      for(size_t i=0; i<size; i++)
      {
        arr[i] = float(i);
      }
    }
    or

    3) Change the argument so that it passes in a pointer to a float array and add another argument that specifies the number of elements:

    Code:
    void assign(float *arr, size_t size)
    {
      if(arr)
      {
        for(size_t i=0; i<size; i++)
        {
          arr[i] = float(i);
        }
      }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured