Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filling up an array in c++ [closed]

I am new to c++ . I was trying to write following code to fill up each byte of array with new values without overriding others. Each byte (r) below should be added up at new address of the array.

int _tmain(int argc, _TCHAR* argv[]) {
    char y[80];
    for(int b = 0; b < 10; ++b) {
        strcpy_s(y, "r");
    }
}

Please let me know if there is any function in c++ which can do that. In the above case the value 'r' is arbitrary and this can have any new value. So the resultant array of characters should contain value rrrrr... 10 times. Thanks a lot in advance for this.

like image 224
junni lomo Avatar asked Jan 21 '13 20:01

junni lomo


People also ask

How do you fill an array in C?

If you want to fill an array of bytes with all the same value, you can use the C library function memset(). It puts the same byte value in all the bytes of your array. If you want a sequence of numbers like your example, though, then no.

Do C arrays need to be freed?

No they don't get freed automatically, but depending on how you allocated each of them, there might be no need to free them actually. You would only need to free them if they point to memory which was returned by malloc and similar allocation functions. Note in this case you don't need to free the array itself.

What is * array [] in C?

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc.

Can you add more elements to an array in C?

We can insert the elements wherever we want, which means we can insert either at starting position or at the middle or at last or anywhere in the array.


4 Answers

Using C++11

#include <algorithm>
#include <iostream>

int main() {
    char array[80];
    std::fill(std::begin(array),std::begin(array)+10,'r');
}

Or, as mentioned in the comments you can use std::fill(array,array+10,'r').

like image 191
Rapptz Avatar answered Oct 26 '22 08:10

Rapptz


You can use the [] operator and assign a char value.

char y[80];
for(int b=0; b<10; ++b)
    y[b] = 'r';

And yes, std::fill is a more idiomatic and modern C++ way to do this, but you should know about the [] operator too!

like image 31
David Heffernan Avatar answered Oct 26 '22 09:10

David Heffernan


Option 1: Initialize the array while defining. Convenient for initializing only a small number of values. Advantage is that the array can be declared const (not shown here).

char const fc = 'r';   // fill char
char y[ 80 ] = { fc, fc, fc, fc,
                 fc, fc, fc, fc,
                 fc, fc };

Option 2: Classic C

memset( y, y+10, 'r' );

Option 3: Classic (pre-C++11) C++

std::fill( y, y+10, 'r' );
like image 2
Arun Avatar answered Oct 26 '22 08:10

Arun


// ConsoleApp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int fun(bool x,int y[],int length);
int funx(char y[]);
int functionx(bool IsMainProd, int MainProdId, int Addons[],int len);
int _tmain(int argc, _TCHAR* argv[])
{
    int AddonCancel[10];

    for( int i = 0 ; i<4 ;++i)
    {
        std::fill(std::begin(AddonCancel)+i,std::begin(AddonCancel)+i+1,i*5);
    }
    bool IsMainProduct (false);
    int MainProduct =4 ; 
    functionx(IsMainProduct,MainProduct,AddonCancel,4);

}

int functionx(bool IsMainProd, int MainProdId, int Addons[],int len)
{
    if(IsMainProd)
        std::cout<< "Is Main Product";
    else
    {
        for(int x = 0 ; x<len;++x)
        {
          std::cout<< Addons[x];
        }
    }

    return 0 ; 
}
like image 1
junni lomo Avatar answered Oct 26 '22 07:10

junni lomo