How to copy char to array of char - c++

I have:
char frame[4][8];
char szBuff[8] = "";
and I want do something like this:
frame[i][j] = szBuff[0];
but it doesnt work:
Access violation reading location 0xcccccccc.

There are several ways to accomplish what (I presume) you are trying to do. Here are three:
#include <cstring>
using std::memcpy;
using std::memset;
#include <algorithm>
using std::fill;
int main() {
char frame[4][8];
char szBuff[8] = "";
// Method 1
for(int i = 0; i < 4; ++i) {
for(int j = 0; j < 8; ++j) {
frame[i][j] = szBuff[0];
}
}
// Method 2
memset(&frame[0][0], szBuff[0], sizeof frame);
// Method 3
// EDIT: Fix end iterator
fill(&frame[0][0], &frame[3][7]+1, szBuff[0]);
}

You are reading outside the bounds of your array more than likely. Debug through it and make sure i and j aren't being incremented outside the bounds of the array you declared. Make sure:
i < 4 and i >= 0
j < 8 and j >= 0

Be sure that your i and j are not out of array...
Example:
i = 5;
j = 7;
frame[i][j] = szBuff[0];
Will not work;
This code:
char frame[4][8];
char szBuff[8] = "1";
frame[1][1] = szBuff[0];
Works fine.

Related

Why does puts() have a random trailing character when using a pointer only for certain values

This is the code
I know that I'm doing something wrong, but I cannot pin down what it is exactly.
#include <cstdio>
int main()
{
int n = 8;
char * string = new char[n];
for (int i = 0; i < n; i ++ )
{
string[i] = '#';
}
puts(pointer);
delete [] pointer;
return 0;
}
For n < 8, I do not run into any issues.
Output for n = 7.
#######
However, when I increase n over 7, it begins to error.
When run multiple times the last character changes, here I represent that with 'A'
Output for n = 8,9,10,11.
########X☺A
#########☺A
##########A
###########
This was confusing, so I wrote a general case. n 0-> 100. I still don't get what is happening.
If you know how I can fix my code, and or why this is happening that would be much appreciated.
You require a trailing '\0', and the buffer must have room for it.
code should be:
#include <cstdio>
int main()
{
int n = 8;
char * pointer = new char[n+1];
for (int i = 0; i < n; i ++ )
{
pointer[i] = '#';
}
pointer[n] = '\0';// note this here, this is the line that does the work
puts(pointer);
delete [] pointer;
return 0;
}

Can I store a string in character array but reverse order, such that 0(index) points to last character in string?

I started to learn to program and I'm trying to store a string into a character array but in reverse order, such that array index position 0(zero) points to the last character in the string. I tried using for loops in different terms but getting the desired output. I want to solve this problem in C++.
Here is the code that I'm stuck on,
#include <iostream>
using namespace std;
int main()
{
char str[maxn];
string entstr;
cin>>entstr;
int len = entstr.length();
cout<<len<<endl;
for(int i = 0 ; i < len ; i++) //this makes \n no sense//
for(int j = len-1 ; j >= 0 ; j--)
str[i] = entstr[j];
for(int i = len-1 ; i >= 0 ; i--)
cout<<"Straight for array "<<i<<" "<<str[i]<<endl;
return 0;
}
OUTPUT:
sachin
6
Straight for array 5 s
Straight for array 4 s
Straight for array 3 s
Straight for array 2 s
Straight for array 1 s
Straight for array 0 s
I guess you don't wanna use std::reverse mentioned in the comments because you want to practice your algorithmic skills.
You can help yourself with this short program. It uses std::malloc to allocate a char array of the size of the input string. Then, you just need to loop backward through the character of the string and store it into your array.
I recommend you to follow (write it down on a paper) the values of i and len-1-i inside the loop. It will help you to understand how index works.
#include <iostream>
using namespace std;
int main()
{
string entstr;
cin >> entstr;
int len = entstr.length();
char* charArray = (char*) malloc (len);
for(int i = 0; i < len; i++)
{
charArray[i] = entstr[len-1-i];
}
cout << "charArray contains: ";
for(int j = 0; j < len; j++)
{
cout << charArray[j];
}
return 0;
}
Good luck!
I don't think there is any use for two loops. Just start the string from the front and your array from len-1 (just the way you did in the for loop for printing). You can take whatever length you want your char array to be.
#include <iostream>
using namespace std;
int main()
{
char str[100]; //put whatever you want the length of the char array to be
string entstr;
cin>>entstr;
int len = entstr.length();
cout<<len<<endl;
for(int i = 0 ; i < len ; i++)
{
str[len-1-i]=entstr[i];
}
for(int i = len-1 ; i >= 0 ; i--)
cout<<"Straight for array "<<i<<" "<<str[i]<<endl;
return 0;
}
You can use two pointers too for the same i.e. one starting from the front and one from the end (if you get confused)
for(int i = 0, j=len-1 ; i < len ; i++, j--)
{
str[j]=entstr[i];
}

How to Intialise on double pointer char array like below?

I'm searching to initialize data of char type like below. but I'm not sure on how to do this
char* data[ ][ ] = {
{"", "index1", "clock1", "Rate1"},
{"", "index2", "clock2", "Rate2"},
{"", "index3", "clock3", "Rate3"},
{"", "index4", "clock4", "Rate4"}
}
so that when I want to assign data of above table to other variables like below
for( int i = 0; i < 6; i++)
{
char k[][];
for(int j = 0; j < 6; j++ )
{
k[i][j] = data[i][j];
}
}
and my expected output is like this
k[i][1] = "index1", k[i][2] = "clock1", k[i][3] = "Rate1"
k[i+1][1] = "index2", k[i+1][2] = "clock2", k[i+1][3] = "Rate2" etc.,
How could I initialize the variable data like above to get the values like mentioned in output?
A couple things to note in the k assignment loop. You are looping 0-6 when the array bounds defined in data are 0-3 (4 elements). You initialized k within your for loop. k will need bounds because it is not being initialized. Remember, arrays in C are of a fixed size. Here is the code I'm referring to:
for( int i = 0; i < 6; i++)
{
char k[][]; /* Re-initialize here? */
for(int j = 0; j < 6; j++ )
{
k[i][j] = data[i][j];
}
}
k will need to be initialized with bounds like this:
char* k[4][4];
Also, your assignment of data is not doing what you think it is. You want to initialize it like this:
char* data[4][4] = {
{{""}, {"index1"}, {"clock1"}, {"Rate1"}},
{{""}, {"index2"}, {"clock2"}, {"Rate2"}},
{{""}, {"index3"}, {"clock3"}, {"Rate3"}},
{{""}, {"index4"}, {"clock4"}, {"Rate4"}}
};
In reality though, this also is not going to be very helpful because, as I said above, arrays are of fixed size. Elements data[0][x] are all single element arrays of char. They will serve no purpose. I think you want to assign different strings to them. Assigning/accessing data[0][x][y] where y is greater than 0 will be undefined behavior. It looks like what you really need here is variable length strings. Since you tagged this question with C++, an easy C++ solution would be to use std::string here. If you really need totally variable length strings in C, you will need to use dynamic memory allocation. For a more simple approach, it would be better to assign data with a fixed string size. Later, if you want to assign different strings, you will want to use strcpy.
Below is a solution for you. I used 20 as the fixed string size. You may want more space.
#include<stdio.h>
int main(int argc, char** argv)
{
char data[4][4][20] = {
{{""}, {"index1"}, {"clock1"}, {"Rate1"}},
{{""}, {"index2"}, {"clock2"}, {"Rate2"}},
{{""}, {"index3"}, {"clock3"}, {"Rate3"}},
{{""}, {"index4"}, {"clock4"}, {"Rate4"}}
};
int i;
int j;
char* k[4][4];
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++ )
k[i][j] = data[i][j];
for (i = 0; i < 4; ++i)
for(j = 0; j < 4; ++j)
printf("%s\n", k[i][j]);
}

Pointer initialization of a struct array

my application crashes when it comes to it.
So I have a struct like this for example(but in reality it has many more things)
struct Record
{
float m_fSimulationTime;
unsigned char m_szflags;
};
In my class I have it declared like this:
Record *m_record[64];
And then I initalize it: (and here the crash occoures (acces violation on read))
void ClassXYZ::initRecord()
{
for (int i = 0; i <= 32; i++)
for (int j = 0; j < 9; j++)
m_record[i][j].m_fSimulationTime = 0.0f; // here happens the crash
}
I hope you can help me out what I'm missing here x.x
Thanks in advice!
The variable m_record is an array of pointers. You need to initialize the pointers first before you access them.
For example:
for (int i = 0; i <= 32; i++)
{
m_record[i] = new Record[9]; // Make the pointer actually point somewhere
for (int j = 0; j < 9; j++)
m_record[i][j].m_fSimulationTime = 0.0f;
}
If the size 9 is fixed at the time of compilation, a better solution would be to use an array of arrays:
Record m_record[64][9];
In this case I would rather recommend using std::array instead though.
If the size of either array is not know at compile-time, but input at run-time then use std::vector instead.

C++ , Array of strings

I was just curious to know how to create an array of strings.
I am looking to make an array of 10 strings and each string can have 20 characters.
#include <iostream>
int main()
{
char a[10] , str[20];
for (int x = 0 ; x<10 ; x++)
{
for (int y = 0 ;y<20; y++ )
{
cout<<"String:";
cin>>str[y];
a[x]=str[y];
}
}
for (int j = 0 ; j<10 ; j++)
cout<<a[j]<<endl;
return 0;
}
Newbie in C++ with an open mind :)
How about instead you use a
std::vector<std::string> my_strings(10); // vector of 10 strings
You will have a much easier time this way than statically-size char arrays.
You then get all the features of the std::vector container, including dynamic size.
You also get all the nice features of the std::string class.
What are you doing is more C approach.
Anyway:
char strings[10][20];
//Accessing each string
for(int i = 0; i < 10; i++)
{
//Accessing each character
for(int j = 0; j < 20; j++)
{
char character = strings[i][j];
}
}
In c++ you would rather use:
std::string strings[10];
Or the best option is:
std::vector<std::string> strings(10);
in c++ 11 you can iterate over last case like this:
for(auto singleString : strings)
{
}
In order of preference:
A vector of 10 strings:
std::vector<std::string> aVector(10);
An array of 10 strings:
std::string anArray[10];
If you really want to use zero-terminated C strings:
typedef char MyString[21]; // 20 + 1, for the terminating zero
MyString arrayOfThem[10];
or, the more cryptic variant
char anArray[10][21];