converting uchar in c++ - c++

How do I convert a uchar to an int?

If uchar is typedef of unsigned char, then it's simple:
unsigned char c = 'A';
int i = c; //automatic conversion uchar into int!
Casting (i.e int i = (int)c) is overkill.

#include <iostream>
int main(int argc, char *argv[])
{
unsigned char x = '\xff';
std::cout << static_cast<int>(x) << std::endl;
return 0;
}
Output:
255

Try:
void func(int x)
{
}
int main()
{
unsigned char c = 'c';
int t = c; // auto converts to int
func(c); // Works the same (auto converts)
func(t); // Or use the t variable.
}

Simply assigning it to a int variable for implicit conversion
unsigned char c = 'c';
int i = c;
Or explicitly type-casting the variable
unsigned char c = 'c';
int i = (int)c;

Related

Problems in using String** pointer for access elemnts of String array

This is a program for using fonts in MCUs. I have 4 files. 'zar.h' and 'zar.cpp' contains data of one font. 'font.h' and 'font.cpp' contains pointers and functions for set fonts dynamically. There is a lot of problems with pointers. When access elements of "XB_Zar_name" array, by "uni_name[i]" pointer, MCU dos reset's. Another problem is counting the strings in "XB_Zar_name" string array. There are definitely other errors that will appear after fixing these errors. Pleas help me.
zar.h
/*v*/extern const unsigned char XB_Zar_118[];
/*w*/extern const unsigned char XB_Zar_119[];
/*x*/extern const unsigned char XB_Zar_120[];
/*y*/extern const unsigned char XB_Zar_121[];
/*z*/extern const unsigned char XB_Zar_122[];
extern char XB_Zar_width[];
extern const unsigned char* XB_Zar_addr[];
extern String XB_Zar_name[];
zar.cpp
/*v*/ const unsigned char XB_Zar_118[] PROGMEM ={255,255,254,255,63,255};
/*w*/ const unsigned char XB_Zar_119[] PROGMEM ={255,127,255,254,255,5};
/*x*/ const unsigned char XB_Zar_120[] PROGMEM ={215,215,23,254,255,127};
/*y*/ const unsigned char XB_Zar_121[] PROGMEM ={255,127,25,254,255,9};
/*z*/ const unsigned char XB_Zar_122[] PROGMEM ={255,243,239,254,215,19};
char XB_Zar_width[]= {2,2,2,2,2};
const unsigned char* XB_Zar_addr[] = {XB_Zar_118,XB_Zar_119,XB_Zar_120,XB_Zar_121,XB_Zar_122};
String XB_Zar_name[] = {"۰","۱","۲","۳","۴","۵"};
font.h
#pragma once
#include "zar.h"
extern unsigned char *char_addr;
extern unsigned int *char_width;
extern String **uni_name;
extern void setFont(byte fontNumber);
extern int getGliphData(String *uniChar, int *index, int *width);
font.cpp
#include "font.h"
unsigned char *char_addr = NULL;
unsigned int *char_width = NULL;
String **uni_name = NULL;
//########################################################
void setFont(byte fontNumber)
{
switch (fontNumber)
{
case 0:
char_addr = (unsigned char *)(XB_Zar_addr);
*uni_name = XB_Zar_name;
char_width = (unsigned int *)XB_Zar_width;
break;
default:
char_addr = (unsigned char *)XB_Zar_addr;
break;
}
}
//########################################################
int getGliphData(String *uniChar, int* index, int* charWidth)
{
Serial.println("uniChar data= "+*uniChar);
Serial.println("uni_name[i]= "+*uni_name[0]);// <---this resets the mcu
int countOfUniChars = sizeof(uni_name) / sizeof(String*);
for (int i = 0; i < countOfUniChars ; i++)
{
if (*uniChar == *uni_name[i])
{
*index = i;
*charWidth = char_width[i];
return i;
}
}
return -1;
}
Inside of getGliphData(), uni_name is just a pointer, so the sizeof() trick you are trying to use to calculate countOfUniChars simply won't work (there are numerous questions on StackOverflow on that very issue).
So, either store the array length into another global variable at the same time you store the array pointer, or else terminate each array with a sentry value (in this case, a blank string) and then loop through the array until you reach the sentry.
Also, there is no need to use a String** pointer when a String* pointer will suffice, as you are iterating through only 1 array of Strings. Were you iterating through an array of pointers to arrays of Strings then String** would make more sense.
Also, another problem I see is that XB_Zar_width[] and XB_Zar_addr[] only have 5 elements in them, but XB_Zar_name[] has 6 elements, and getGliphData() is trying to loop through XB_Zar_name[] using its indexes to access the elements of XB_Zar_width[] and XB_Zar_addr[], which will obviously not work for the 6th element.
That being said, try something more like this instead:
// zar.h
/*v*/extern const unsigned char XB_Zar_118[];
/*w*/extern const unsigned char XB_Zar_119[];
/*x*/extern const unsigned char XB_Zar_120[];
/*y*/extern const unsigned char XB_Zar_121[];
/*z*/extern const unsigned char XB_Zar_122[];
extern const unsigned int XB_Zar_width[];
extern const unsigned char* XB_Zar_addr[];
extern const String XB_Zar_name[];
extern const int XB_Zar_name_len;
//zar.cpp
/*v*/ const unsigned char XB_Zar_118[] PROGMEM ={255,255,254,255,63,255};
/*w*/ const unsigned char XB_Zar_119[] PROGMEM ={255,127,255,254,255,5};
/*x*/ const unsigned char XB_Zar_120[] PROGMEM ={215,215,23,254,255,127};
/*y*/ const unsigned char XB_Zar_121[] PROGMEM ={255,127,25,254,255,9};
/*z*/ const unsigned char XB_Zar_122[] PROGMEM ={255,243,239,254,215,19};
const unsigned int XB_Zar_width[] = {2, 2, 2, 2, 2, 0}; // TODO: fix the 6th element!
const unsigned char* XB_Zar_addr[] = {XB_Zar_118, XB_Zar_119, XB_Zar_120, XB_Zar_121, XB_Zar_122, NULL}; // TODO: fix the 6th element!
const String XB_Zar_name[] = {"۰", "۱", "۲", "۳", "۴", "۵"}; // TODO: remove the 6th element?
const int XB_Zar_name_len = sizeof(XB_Zar_name)/sizeof(XB_Zar_name[0]);
//font.h
#pragma once
#include "zar.h"
extern const unsigned char **char_addr;
extern const unsigned int *char_width;
extern const String *uni_name;
extern int uni_name_len;
extern void setFont(byte fontNumber);
extern int getGliphData(const String &uniChar, int *index, int *width);
//font.cpp
#include "font.h"
const unsigned char **char_addr = NULL;
const unsigned int *char_width = NULL;
const String *uni_name = NULL;
int uni_name_len = 0;
//########################################################
void setFont(byte fontNumber)
{
switch (fontNumber)
{
case 0:
char_addr = XB_Zar_addr;
char_width = XB_Zar_width;
uni_name = XB_Zar_name;
uni_name_len = XB_Zar_name_len;
break;
default:
char_addr = XB_Zar_addr;
char_width = XB_Zar_width;
break;
}
}
//########################################################
int getGliphData(const String &uniChar, int* index, int* charWidth)
{
Serial.println("uniChar data= " + uniChar);
if (uni_name)
{
for (int i = 0; i < uni_name_len; ++i)
{
Serial.println("uni_name[" + String(i) + "]= " + uni_name[i]);
if (uniChar == uni_name[i])
{
*index = i;
*charWidth = char_width[i];
return i;
}
}
}
return -1;
}
Online Demo
Alternatively:
// zar.h
/*v*/extern const unsigned char XB_Zar_118[];
/*w*/extern const unsigned char XB_Zar_119[];
/*x*/extern const unsigned char XB_Zar_120[];
/*y*/extern const unsigned char XB_Zar_121[];
/*z*/extern const unsigned char XB_Zar_122[];
extern const unsigned int XB_Zar_width[];
extern const unsigned char* XB_Zar_addr[];
extern const String XB_Zar_name[];
//zar.cpp
/*v*/ const unsigned char XB_Zar_118[] PROGMEM ={255,255,254,255,63,255};
/*w*/ const unsigned char XB_Zar_119[] PROGMEM ={255,127,255,254,255,5};
/*x*/ const unsigned char XB_Zar_120[] PROGMEM ={215,215,23,254,255,127};
/*y*/ const unsigned char XB_Zar_121[] PROGMEM ={255,127,25,254,255,9};
/*z*/ const unsigned char XB_Zar_122[] PROGMEM ={255,243,239,254,215,19};
const unsigned int XB_Zar_width[] = {2, 2, 2, 2, 2, 0}; // TODO: fix the 6th element!
const unsigned char* XB_Zar_addr[] = {XB_Zar_118, XB_Zar_119, XB_Zar_120, XB_Zar_121, XB_Zar_122, NULL}; // TODO: fix the 6th element!
const String XB_Zar_name[] = {"۰", "۱", "۲", "۳", "۴", "۵", ""}; // TODO: remove the 6th element?
//font.h
#pragma once
#include "zar.h"
extern const unsigned char **char_addr;
extern const unsigned int *char_width;
extern const String *uni_name;
extern void setFont(byte fontNumber);
extern int getGliphData(const String &uniChar, int *index, int *width);
//font.cpp
#include "font.h"
const unsigned char **char_addr = NULL;
const unsigned int *char_width = NULL;
const String *uni_name = NULL;
//########################################################
void setFont(byte fontNumber)
{
switch (fontNumber)
{
case 0:
char_addr = XB_Zar_addr;
char_width = XB_Zar_width;
uni_name = XB_Zar_name;
break;
default:
char_addr = XB_Zar_addr;
char_width = XB_Zar_width;
break;
}
}
//########################################################
int getGliphData(const String &uniChar, int* index, int* charWidth)
{
Serial.println("uniChar data= " + uniChar);
if (uni_name)
{
for (int i = 0; uni_name[i].Length() > 0; ++i)
{
Serial.println("uni_name[" + String(i) + "]= " + uni_name[i]);
if (uniChar == uni_name[i])
{
*index = i;
*charWidth = char_width[i];
return i;
}
}
}
return -1;
}
Online Demo
I have completed #RemyLebeau excellent answer for running on ESP32 microcontroller.
// zar.h
/*v*/extern const unsigned char XB_Zar_118[];
/*w*/extern const unsigned char XB_Zar_119[];
/*x*/extern const unsigned char XB_Zar_120[];
/*y*/extern const unsigned char XB_Zar_121[];
/*z*/extern const unsigned char XB_Zar_122[];
extern const unsigned int XB_Zar_width[];
extern const unsigned char* XB_Zar_addr[];
extern const String XB_Zar_name[];
extern const int XB_Zar_name_len;
zar.cpp file:
//zar.cpp
/*v*/ const unsigned char XB_Zar_118[] PROGMEM ={255,255,254,255,63,255};
/*w*/ const unsigned char XB_Zar_119[] PROGMEM ={255,127,255,254,255,5};
/*x*/ const unsigned char XB_Zar_120[] PROGMEM ={215,215,23,254,255,127};
/*y*/ const unsigned char XB_Zar_121[] PROGMEM ={255,127,25,254,255,9};
/*z*/ const unsigned char XB_Zar_122[] PROGMEM ={255,243,239,254,215,19};
const unsigned int XB_Zar_width[] = {2, 2, 2, 2, 2, 0}; // TODO: fix the 6th element!
const unsigned char* XB_Zar_addr[] = {XB_Zar_118, XB_Zar_119, XB_Zar_120, XB_Zar_121, XB_Zar_122, NULL}; // TODO: fix the 6th element!
const String XB_Zar_name[] = {"۰", "۱", "۲", "۳", "۴", "۵"}; // TODO: remove the 6th element?
const int XB_Zar_name_len = sizeof(XB_Zar_name)/sizeof(XB_Zar_name[0]);
font.h file:
//font.h
#pragma once
#include "zar.h"
extern const unsigned char **char_addr;
extern const unsigned int *char_width;
extern const String *uni_name;
extern void setFont(byte fontNumber);
extern int getGliphData(const String &uniChar, int *index, int *width);
font.cpp file:
//font.cpp
#include "font.h"
const unsigned char **char_addr = NULL;
const unsigned int *char_width = NULL;
const String *uni_name = NULL;
//########################################################
void setFont(byte fontNumber)
{
switch (fontNumber)
{
case 0:
char_addr = (unsigned char *)pgm_read_ptr(XB_Zar_addr[0]); // use pgm_read_ptr() becuse there is PROGMEM space in flash memory
char_addr = XB_Zar_addr;
char_width = XB_Zar_width;
uni_name = XB_Zar_name;
break;
default:
char_addr = (unsigned char *)pgm_read_ptr(XB_Zar_addr[0]); // use pgm_read_ptr() becuse there is PROGMEM space in flash memory
char_addr = XB_Zar_addr;
char_width = XB_Zar_width;
uni_name = XB_Zar_name;
break;
}
//Now. char_addr points to flash space and no need to use pgm_read_word_near()
// or using pgm_read_byte_near(). Simply use "*(char_addr)" or "*(char_addr+number)"
//Serial.println("char_addr[0]= "+String(*(char_addr))));
//Serial.println("char_addr[1]= "+String(*(char_addr+1)));
//Serial.println("char_addr[3]= "+String(*(char_addr+3)));
}
//########################################################
int getGliphData(const String &uniChar, int* index, int* charWidth)
{
Serial.println("uniChar data= " + uniChar);
if (uni_name)
{
for (int i = 0; uni_name[i].Length() > 0; ++i)
{
Serial.println("uni_name[" + String(i) + "]= " + uni_name[i]);
if (uniChar == uni_name[i])
{
*index = i;
/* *charWidth = char_width[i]; it works if 'i' is
* a const number at compile time.
* constant 'i' only works because the compiler
* sets the value of char_width[i] during
* compilation, char_width[i] is a constant value
* pointing to one specific location in progmem
* that the compiler knows the value of while
* compiling see:https://forum.arduino.cc/t/passing-a-pointer-to-progmem-in-function/637756/5
*/
*charWidth = pgm_read_word_near(char_width+*index);
return i;
}
}
}
return -1;
}

How to allocate memory for array of structs?

I've a C++ library (really a wrapper for another C++ library) and I need pass some structs to my C application.
I don't know how allocate the memory dynamically.
I get a segmentation fault.
library.h
struct my_substruct {
unsigned char id ;
time_t date ;
char *info ;
};
typedef struct my_substruct My_substruct ;
struct my_struct {
char *description ;
unsigned char value ;
My_substruct *substruct ;
};
typedef my_struct My_struct ;
library.cpp
unsigned char getStructs(My_struct *structs)
{
vector <structCPPLibrary> structsCPPLibrary = getFromCPPLibrary();
unsigned char numStructs = structsCPPLibrary.size();
structs = (My_struct *)malloc(numStructs*sizeof(My_struct));
unsigned char indexStruct = 0;
for (auto s : structsCPPLibrary)
{
structs[indexStruct].description = (char *)malloc(s.description.size() + 1);
strcpy(structs[indexStruct].description, s.description.c_str()); // In 's' is a std::string
structs[indexStruct].value = s.value; // In 's' is an unsigned char too
unsigned char numSubstructs = s.substructs.size(); // In 's' is a vector of Substructs
structs[indexStruct].substruct = (My_substruct *)malloc(numSubstructs*sizeof(My_substruct));
unsigned char indexSubstruct = 0;
for (auto sub : s.substruct) {
structs[indexStruct].substruct[indexSubstruct].id = sub.id; // In 'sub' is an unsigned char too
structs[indexStruct].substruct[indexSubstruct].date = sub.date; // In 'sub' is a time_t too
structs[indexStruct].substruct[indexSubstruct].info = (char *)malloc(sub.info.size() + 1);
strcpy(structs[indexStruct].substruct[indexSubstruct].info, sub.info.c_str()); // In 'sub' is a std::string
indexSubstruct++;
}
indexStruct++;
}
return indexStruct;
}
main.c
void getStructFromWrapper(void)
{
My_struct *structs;
unsigned char numStruct = getStructs(structs);
show_content(structs);
}
Change
unsigned char getStructs(My_struct *structs) {
...
}
getStructs(structs);
To
unsigned char getStructs(My_struct **p_structs) {
// C function can't be pass by reference, so convert to a reference here
auto& struct = *p_structs;
...
}
...
getStructs(&structs);
Your problem is that your struct = ... line is not modifying the value of structs in getStructFromWrapper.

MessageBox string with variable name value dev c++ [duplicate]

I have the following code and would like to end up with a char such as: "Hello, how are you?" (this is just an example of what I'm trying to achieve)
How can I concatenate the 2 char arrays plus adding the "," in the middle and the "you?" at the end?
So far this concatenates the 2 arrays but not sure how to add the additional characters to my final char variable I want to come up with.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char foo[] = { "hello" };
char test[] = { "how are" };
strncat_s(foo, test, 12);
cout << foo;
return 0;
}
EDIT:
This is what I came up with after all your replies. I'd like to know if this is the best approach?
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char foo[] = { "hola" };
char test[] = { "test" };
string foos, tests;
foos = string(foo);
tests = string(test);
string concat = foos + " " + tests;
cout << concat;
return 0;
}
In C++, use std::string, and the operator+, it is designed specifically to solve problems like this.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string foo( "hello" );
string test( "how are" );
cout << foo + " , " + test;
return 0;
}
Best thing is use std::string in C++ as other answers. If you really need to work with char try this way. didn't tested.
const char* foo = "hello";
const char* test= "how are";
char* full_text;
full_text= malloc(strlen(foo)+strlen(test)+1);
strcpy(full_text, foo );
strcat(full_text, test);
If you dont want to use string you can do it simply by taking an other array and storing both arrays in it with loop
#include<iostream>
#include<stdlib.h>
#include<cstdio>
using namespace std;
int main(){
char fname[30],lname[30],full_name[60];
int i,j;
i=0;j=0; // i is index of fname and j is index for lname
cout<<"Enter your first name: ";
gets(fname);
cout<<"Enter your last name: ";
gets(lname);
for (i;fname[i]!='\0';i++){
full_name[i] = fname[i];
}
cout<<"i ="<<i;
full_name[i]=' ';
i = i + 1;
for (i,j;lname[j]!='\0';i++,j++){
full_name[i] = lname[j];
}
cout<<"Your full name is: "<<full_name<<endl;
system("pause");
return 0;
}
Yes, in C++ use the + operator for string concatenation.
But this will not work:
char[] + char[] + char[]
convert one array to std::string and it will:
std::string(char[]) + char[] + char[]
E.g.:
#include <iostream>
int main()
{
const char a[] = "how ";
const char b[] = "are ";
const char c[] = "you ";
std::cout << std::string( a + b + c ) << "\n"; // Error
std::cout << std::string(a) + b + c << "\n"; // Fine
}
If performance is a concern for you, I would suggest avoiding std::string. Instead, you can use the character array.
template <typename Result>
void concatenate(Result *res)
{
return;
}
template <typename Result, typename T>
void concatenate(Result *res, T *str)
{
strcat(res, str);
}
template <typename Result, typename First, typename ... T>
void concatenate(Result *res, First *f, T* ... next)
{
strcat(res, f);
concatenate(res, next...);
}
template <typename Result, typename First, typename ... T>
void concatStrings(Result *res, First *f, T* ... next)
{
strcpy(res, f);
concatenate(res, next...);
}
And then, you can call the concatStrings function with at least two parameters and at most as many you need.
/* You can remove constexpr as per your need. */
constexpr char hello[6] = "hello";
constexpr char sep[2] = ",";
constexpr char how[5] = " how";
constexpr char are[5] = " are";
constexpr char you[6] = " you?";
auto totalSize = strlen(hello) + strlen(sep) + strlen(how) + strlen(are) + strlen(you) + 5;
char statement[totalSize];
concatStrings(statement, hello, sep, how, are, you);
std::cout << statement << '\n';
cout<<x<<y<<z<<" ";
char arr[3] = {x , y ,z};
ans.push_back(arr);
if you want to push in vector array.
FOR 2 CHAR ARRAYS
char foo[] = { "hello " };
char test[] = { "how are" };
char concat[50];
char x='X'; //Any Temporary Variable
int i;
for(i=0; x!='\0'; i++){ //x is not NULL
x = foo[i];
concat[i] = x;
}
x = 'D';
i--;
for (int k = 0; x!='\0'; i++){
x = test[k];
concat[i] = x;
k++;
}
cout<<"Concat Array is: "<<concat;

Concatenate char arrays in C++

I have the following code and would like to end up with a char such as: "Hello, how are you?" (this is just an example of what I'm trying to achieve)
How can I concatenate the 2 char arrays plus adding the "," in the middle and the "you?" at the end?
So far this concatenates the 2 arrays but not sure how to add the additional characters to my final char variable I want to come up with.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char foo[] = { "hello" };
char test[] = { "how are" };
strncat_s(foo, test, 12);
cout << foo;
return 0;
}
EDIT:
This is what I came up with after all your replies. I'd like to know if this is the best approach?
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char foo[] = { "hola" };
char test[] = { "test" };
string foos, tests;
foos = string(foo);
tests = string(test);
string concat = foos + " " + tests;
cout << concat;
return 0;
}
In C++, use std::string, and the operator+, it is designed specifically to solve problems like this.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string foo( "hello" );
string test( "how are" );
cout << foo + " , " + test;
return 0;
}
Best thing is use std::string in C++ as other answers. If you really need to work with char try this way. didn't tested.
const char* foo = "hello";
const char* test= "how are";
char* full_text;
full_text= malloc(strlen(foo)+strlen(test)+1);
strcpy(full_text, foo );
strcat(full_text, test);
If you dont want to use string you can do it simply by taking an other array and storing both arrays in it with loop
#include<iostream>
#include<stdlib.h>
#include<cstdio>
using namespace std;
int main(){
char fname[30],lname[30],full_name[60];
int i,j;
i=0;j=0; // i is index of fname and j is index for lname
cout<<"Enter your first name: ";
gets(fname);
cout<<"Enter your last name: ";
gets(lname);
for (i;fname[i]!='\0';i++){
full_name[i] = fname[i];
}
cout<<"i ="<<i;
full_name[i]=' ';
i = i + 1;
for (i,j;lname[j]!='\0';i++,j++){
full_name[i] = lname[j];
}
cout<<"Your full name is: "<<full_name<<endl;
system("pause");
return 0;
}
Yes, in C++ use the + operator for string concatenation.
But this will not work:
char[] + char[] + char[]
convert one array to std::string and it will:
std::string(char[]) + char[] + char[]
E.g.:
#include <iostream>
int main()
{
const char a[] = "how ";
const char b[] = "are ";
const char c[] = "you ";
std::cout << std::string( a + b + c ) << "\n"; // Error
std::cout << std::string(a) + b + c << "\n"; // Fine
}
If performance is a concern for you, I would suggest avoiding std::string. Instead, you can use the character array.
template <typename Result>
void concatenate(Result *res)
{
return;
}
template <typename Result, typename T>
void concatenate(Result *res, T *str)
{
strcat(res, str);
}
template <typename Result, typename First, typename ... T>
void concatenate(Result *res, First *f, T* ... next)
{
strcat(res, f);
concatenate(res, next...);
}
template <typename Result, typename First, typename ... T>
void concatStrings(Result *res, First *f, T* ... next)
{
strcpy(res, f);
concatenate(res, next...);
}
And then, you can call the concatStrings function with at least two parameters and at most as many you need.
/* You can remove constexpr as per your need. */
constexpr char hello[6] = "hello";
constexpr char sep[2] = ",";
constexpr char how[5] = " how";
constexpr char are[5] = " are";
constexpr char you[6] = " you?";
auto totalSize = strlen(hello) + strlen(sep) + strlen(how) + strlen(are) + strlen(you) + 5;
char statement[totalSize];
concatStrings(statement, hello, sep, how, are, you);
std::cout << statement << '\n';
cout<<x<<y<<z<<" ";
char arr[3] = {x , y ,z};
ans.push_back(arr);
if you want to push in vector array.
FOR 2 CHAR ARRAYS
char foo[] = { "hello " };
char test[] = { "how are" };
char concat[50];
char x='X'; //Any Temporary Variable
int i;
for(i=0; x!='\0'; i++){ //x is not NULL
x = foo[i];
concat[i] = x;
}
x = 'D';
i--;
for (int k = 0; x!='\0'; i++){
x = test[k];
concat[i] = x;
k++;
}
cout<<"Concat Array is: "<<concat;

How to declare the Char array of char variables already declared in class?

#include <stdio.h>
#include <iostream>
using namespace std;
//char* b[6] = new char[6];
char a[6] = {'b','c','d','e','f','g'};
char c[6] = {'a','b','d','d','f','g'};
int main()
{
char d[][6]={*a,*c};
for (int x = 0 ; x < 1; x++)
{
for(int y = 0; y<6; y++)
{
char test = d[x][y];
cout << test <<"\n";
}
}
return 0;
}
This code is C++ code. I am trying to create a class where it stores the char array. Then there is another char array of array storing already declared char variables. It compiles fine but it doesn't work out to as it should. It doesn't get me the right value that it should when the program tries to print the value
May be you meant array of pointers:
char *d[]={a,c};
typedef std::vector<char> VectorChar;
typedef std::vector< VectorChar* > VectorVectorChar;
struct V
{
V() : _v{ '0', '1', '2' } {}
VectorChar _v;
};
int main(void)
{
V arrV[5];
VectorVectorChar vvc;
for ( auto& v : arrV )
vvc.push_back( &v._v );
// print them
for ( auto pV : vvc )
{
for ( auto c : *pV )
cout << c << ' ';
cout << '\n;
}
return 0;
}
what i understood from the question that, you want to create class to store char array, which already initialized.
#include <stdio.h>
#include <iostream>
char a[6] = {'b','c','d','e','f','g'}; // Initialized character array. MAX 6
// This class will hold char array
class Test {
public:
void setName(char *name);
const char* getName();
private:
char m_name[6]; // MAX 6 ( since you already initialized(MAX 6), So no need dynamic memory allocation. )
};
void Test::setName(char *name) {
strcpy(m_name, name); // Copy, already initialized array
}
const char* Test::getName() {
return m_name;
}
int main(int argc, char** argv) {
{
Test foobar;
foobar.setName( a ); // set the pointer which point to starting of the initialized array.
cout << foobar.getName();
return 0;
}
char a[6] = {'b','c','d','e','f','\0'};
char c[6] = {'a','b','d','d','f','\0'};
char* d[]= {a,c};
for (int x = 0 ; x < 2; x++)
{
for(int y = 0; y < 6; y++)
{
char test = d[x][y];
cout << test << "\n";
}
}
return 0;