2

I am stuck with basics of an array. I googled it, but I could not able to solve this. This question is concept oriented, but I am not able to bridge a gap. Please help me out resolve this.

I am very clear on initializing array for the first time, but not able to clear the array for next time :-( :-(

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }; // All elements of myArray are 5
int myArray[10] = { 0 };    // Will initialize all elements to 0
int myArray[10] = { 5 };    // Will initialize myArray[0] to 5 and other elements to 0
static int myArray[10]; // Will initialize all elements to 0

int myArray[10];// This will declare and define (allocate memory) but won’t initialize
int i;  // Loop variable
for (i = 0; i < 10; ++i) // Using for loop we are initializing
{
    myArray[i] = 5;
}

Problem statement:

I am using a Global array which is of size 10000. I have 1k iteration loop, where I have to set the array to 'Zero' or predefined value i.e., 5 for each iteration.

I am doing this at the end of interaction like this:

myArray[10000] = { 0 };

Its giving error while compiling in GCC compiler.

error: [11707] syntax error before '{' token

Please help me out resolve this.

Thanks in advance.

9
  • 1
    Isn't memset() for you? ... memset() won't able to set each values to 5. Commented May 8, 2016 at 14:43
  • 1
    memset, loop with incementing index or pointer, bzero... . maybe you should just move to a different course? Commented May 8, 2016 at 14:44
  • 1
    @ Martin, I can't use Memset, because od some constraint. Is there any other way to achieve this? Commented May 8, 2016 at 14:46
  • I really don't understand, y this down votes. Is anyone can explain? Commented May 8, 2016 at 14:48
  • 1
    If you're re-initializing your array on each iteration, you may as well declare the array inside the loop. Commented May 8, 2016 at 14:50

2 Answers 2

4
myArray[10000] = { 0 };

is an assignment, not a declaration, so it won't work. You should loop.

int myArray[10000] = { 0 };

for(int i = 0; i < 1000; i++) {
    for (int j = 0; j < 10000; j++) {
        myArray[j] = ;  // put your value here.
    }
}

I am unsure of the purpose of this. Benchmarking?

Sign up to request clarification or add additional context in comments.

Comments

0

The trivial implementation of memset, without any optimization, is a loop that set each position of an array to a desired value. The problem is memset do this for each byte, and it is not what you want. I recommend you to implement an alternative to memset:

int *mymemset (int *s, int v, size_t n) {
    unsigned int *p = s;
    while(n--) {
        *p++ = (unsigned int)v;
    }

    return s;
}

And use it to initialize your array.

8 Comments

But my array is of global scope(mentioned in question), not an auto/ local scope. I am using array for many other data manipulation.
oh.. ok. So you have a global array that you want to initialize with 0s or whatever 1k times? Isn't this a design problem?
Yes, but I have to strongly go with this option, because of data processing in multiple parts of the code.
Thanks alot!!, But not able to upvote, because I have less than 15 reputation. Btw I have to call mymemset (myArray, 0, 10000); Is n't it?
Yes. Or mymemset(myArray, 5, 10000);. Try and see if this solves your problem. If it does, accept as an answer. Please note that *p++ = (unsigned int)v; won't be able to set negative values. Remove the cast if you want to use negative numbers.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.