|
-
April 15th, 2009, 05:10 PM
#1
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.
-
April 15th, 2009, 05:14 PM
#2
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.
-
April 15th, 2009, 05:37 PM
#3
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.
-
April 15th, 2009, 05:48 PM
#4
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....?
-
April 15th, 2009, 06:15 PM
#5
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.
-
April 15th, 2009, 06:41 PM
#6
Re: question about assigning values to a array member.
 Originally Posted by 0xC0000005
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.
-
April 15th, 2009, 07:27 PM
#7
Re: question about assigning values to a array member.
 Originally Posted by 0xC0000005
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% 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.
-
April 15th, 2009, 08:53 PM
#8
Re: question about assigning values to a array member.
 Originally Posted by Lindley
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
-
April 16th, 2009, 07:29 AM
#9
Re: question about assigning values to a array member.
 Originally Posted by billconan
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|