Need help copying a char array into an array struct with char pointer - pointers

I'm trying to copy char array word to char pointer s[1].c, an then another word to char pointer s[2].c but when i'm trying to do that , the second word appears to be copied in all two pointers . How can i fix that ? I don't want to use strings .
struct Stud {
char *c;
} s[100];
char word[32];
int main()
{
strcpy(word,"one");
s[1].c=word;
word={0};
strcpy(word,"two");
s[2].c=word;
cout<<s[1].c<<" "<<s[2].c;
return 0;
}

In your code you are setting s[1].c = word; which means you are setting s[1].c to the address of word. Then you set s[2].c = word; which is the same exact memory location. (With c strings, (char *)s1 = (char *)2 does not do a string copy as you might expect. It just assigns one pointer to another).
With strdup you allocate a new memory block and then copy the string into the allocated space.
Here's your code modified.
struct Stud
{
char *c;
} s[100];
int main()
{
char word[32];
strcpy(word, "one");
s[0].c = strdup(word); // In C/C++ the first array index is 0
strcpy(word, "two");
s[1].c = strdup(word);
// Should check to make sure s[0].c and s[1].c are not NULL....
cout << s[0].c << " " <<s [1].c;
free(s[0].c);
free(s[1].c);
return 0;
}

Related

QT String to char * adds extra characters

I have a qTextEdit that I grab the text from (QString) and convert to a char* with this code:
QString msgQText = ui->textMsg->toPlainText();
size_t textSize = (size_t)msgQText.size();
if (textSize > 139) {
textSize = 139;
}
unsigned char * msgText = (unsigned char *)malloc(textSize);
memcpy(msgText, msgQText.toLocal8Bit().data(), textSize);
msgText[textSize] = '\0';
if (textSize > 0) {
Msg * newTextMsg = new Msg;
newTextMsg->type = 1; // text message type
newTextMsg->bitrate = 0;
newTextMsg->samplerate = 0;
newTextMsg->bufSize = (int)textSize;
newTextMsg->len = 0;
newTextMsg->buf = (char *)malloc(textSize);
memcpy((char *)newTextMsg->buf, (char *)msgText, textSize);
lPushToEnd(sendMsgList, newTextMsg, sizeof(Msg));
ui->sendRecList->addItem((char *)newTextMsg->buf);
ui->textMsg->clear();
}
I put the text into a qListBox, but it shows up like
However, the character array, if I print it out, does not have the extra characters.
I have tried checking the "compile using UTF-8" option, but it doesn't make a difference.
Also, I send the text using RS232, and the receiver side also displays the extra characters.
The receiver code is here:
m_serial->waitForReadyRead(200);
const QByteArray data = m_serial->readAll();
if (data.size() > 0) {
qDebug() << "New serial data: " << data;
QString str = QString(data);
if (str.contains("0x6F8C32E90A")) {
qDebug() << "TEST SUCCESSFUL!";
}
return data.data();
} else {
return NULL;
}
There is a difference between the size of a QString and the size of the QByteArray returned by toLocal8Bit(). A QString contains unicode text stored as UTF-16, while a QByteArray is "just" a char[].
A QByteArray is null-terminated, so you do not need to add it manually.
As #GM pointed out: msgText[textSize] = '\0'; is undefined behavior. You are writing to the textSize + 1 position of the msgText array.
This position may be owned by something else and may be overwritten, so you end up with a non null terminated string.
This should work:
QByteArray bytes = msgQText.toLocal8Bit();
size_t textSize = (size_t)bytes.size() + 1; // Add 1 for the final '\0'
unsigned char * msgText = (unsigned char *) malloc(textSize);
memcpy(msgText, bytes.constData(), textSize);
Additional tips:
Prefer using const functions on Qt types that are copy-on-write, e.g. use QBytearray::constData() instead of QByteArray::data(). The non-const functions can cause a deep-copy of the object.
Do not use malloc() and other C-style functions if possible. Here you could do:
unsigned char * msgText = new unsigned char[textSize]; and later delete[] msgText;.
Prefer using C++ casts (static_cast, reinterpret_cast, etc.) instead of C-style casts.
You are making 2 copies of the text (2 calls to memcpy), given your code only 1 seem to be enough.

Best way to interleave two char arrays

I am looking for the best way to interleave two char arrays using a function that were input by the user. I am learning C++ and this is a homework so I can't use anything too advance assignment I am just curious if I am going the right direction with it.
I was thinking that using a for loop to iterate through each index in both arrays and then use the function to add the two chars and output them. I also have to use pointers for this assignment.
#include <iostream>;
using namespace std;
char* strinterleave(const char *a, const char *b) {
char done;
done = *a + *b;
}
int main() {
char interleave[]="";
char storage1[] = "";
char storage2[] = "";
int lena;
int lenb;
char *a_ptr = storage1;
char *b_ptr = storage2;
cin.getline(storage1, numeric_limits<streamsize>::max(), ' ');
cin.getline(storage2, numeric_limits<streamsize>::max());
lena = strlen(a_ptr);
lenb = strlen(b_ptr);
int finallen = lena + lenb;
for (int j = 0; j <= finallen; ++j) {
strinterleave(a_ptr[j], b_ptr[j]);
}
}
Is this the wrong way to go about this and if so what would be the right direction to go?
Thank you
Here are some things to consider:
The string lengths may not be the same. You'll need to decide what do with any remaining characters in the longer string.
Examining your code...
int finallen = lena + lenb;
for (int j = 0; j <= finallen; ++j) { <--- Remember you are processing two characters at once, so you probably want something like the max of lena or lenb.
strinterleave(a_ptr[j], b_ptr[j]); // <--- this is passing chars, not pointers to chars, so it doesn't match the function declaration.
char* strinterleave(const char *a, const char b) { //<- Make this just chars, not char)
char done;
done = *a + *b; <-- Adding the characters won't interleave them. I'm guessing your instructor wants a new string created with the characters interleaved.
For instance if you have:
storage1 = "123456"
storage2 = "abc"
The instructor probably wants:
finalString = "1a2b3c456"
}

seg fault / pointer assistance

so i know the bases of programming, i have a decent amount of experience with java, but im learning C for school right now. I still dont completely understand the whole pointer aspect, which is what im sure caused the fault. This program works fine when run on my computer, but when i try and run it on my schools unix shell it gives me a seg fault. if someone could please explain to me why or how ive misused hte pointers, that would help me greatly.
//Matthew Gerton
//CS 222 - 002
//10/10/14
//HW Six
//libraries
#include<stdio.h>
#include<string.h>
#define max_Length 256
//prototypes
void decode(char *a, char *b);
void trimWhite(char *a);
void encode(char *a, char *b);
int main(void)
{
//character arrays
char coded[max_Length], decoded[max_Length];
//decode the sample phrase
char sample[] = {'P','H','H','W','D','W','C','R','R','F','D','Q','F','H','O','H','G','J',
'R','W','R','P','H','W','U','R','K','R','W','H','O','U','R','R','P','I','R','X','U'};
decode(sample, decoded);
//scans a user input string to decode, and decodes it
printf("\nPlease enter a phrase to decode: ");
gets(coded);
trimWhite(coded);
decode(coded, decoded);
//scans a user input phrase to encode
printf("\nPlease enter a phrase to encode: ");
gets(coded);
trimWhite(coded);
encode(coded, decoded);
}
//removes any spaces from the input
void trimWhite(char *a)
{
char temp[max_Length];
int z=0, y=0;
while(a[z]!='\0')
{
if(a[z]!=' ')
{
temp[y]=a[z];
y++;
}
z++;
}
temp[y] = '\0';
strcpy(a,temp);
}
//decodes any phrase
void decode(char *a, char *b)
{
int i=0,n;
memset(b, '\0', sizeof(b));
while(a[i]!='\0')
{
n=(int)a[i];
if(n<97)
n=n+32;
if(n<=99)
n=n+23;
else
n = n-3;
b[i]= (char) n;
i++;
}
b[i]='\0';
printf("Coded message: %s\n", a);
printf("Decoded message: %s\n", b);
}
//codes an input phrase
void encode(char *a, char *b)
{
int i=0,n;
memset(b, '\0', sizeof(b));
strcpy(b,a);
while(a[i]!='\0')
{
n=(int)a[i];
if(n<97)
a[i] = (char)(n+32);
if((n>120)
a[i] = (char)(n-23);
else
a[i] = (char)((n+3);
i++;
}
printf("Coded message: %s\n", a);
}
Your main problem is here:
char sample[] = {'P','H','H', /* snip */ ,'R','X','U'};
The sample[] array is not zero-terminated which may cause the decode() function to copy many more characters than intended, thus overwriting other variables. You need to explicitly add a terminating zero when using an initializer-list:
char sample[] = {'P','H','H', /* ... */ ,'R','X','U',0};
Or you can initialize the array using a string literal, which does include a terminating zero:
char sample[] = "PHHWDWCRRFDQFHOHGJRWRPHWURKRWHOURRPIRXU";
You should probably read "Why is the gets function dangerous".
...
void decode(char *a, char *b)
{
int i=0,n;
memset(b, '\0', sizeof(b));
Also note that the size of the array is lost when it is passed to a function. The function only receives a pointer to its first element. The memset() call above will only zero sizeof(char*) bytes (usually 4 or 8). This doesn't matter though because, as far as I can tell, you only need to zero the first byte. You could simply write:
b[0] = 0;

Printing Address of Struct Element

I have the following struct:
typedef struct Author
{
char** novels;
} Author;
And I want to print the address of an element in the novels array. I tried these two:
printf("%p\n", &(herbert->novels[1]));
printf("%p\n", herbert->novels[1]);
But I'm not sure which is correct. Can someone help me understand which to use and why?
Take a look at the below...
typedef struct Author
{
char** novels;
} Author;
int main()
{
Author a;
char b = 'b';
a.novels = new char*[2];
a.novels[0] = NULL;
a.novels[1] = NULL;
printf("1. %p\n", a.novels[1]);
printf("2. %p\n", &(a.novels[1]));
delete[] a.novels;
return 0;
}
this outputs the following
1. 0000000000000000
2. 00000000001269C8
You can see the first print is actually a NULL - which is the value stored at the a.novels[1].
The second is the address of the a.novels[1] memory.
Assuming you look for the memory address of the item, you'll need the second syntax
printf("%p\n", &(herbert->novels[1]));

QByteArray convert to/from unsigned char *

QByteArray inArray = " ... ";
unsigned char *in = convert1(inArray);
unsigned char *out;
someFunction(in, out);
QByteArray outArray = convert2(out);
the question is how can I correctly make these conversions (convert1 and convert2).
I cannot change someFunction(unsigned char *, unsigned char *), but I have to work with QByteArray here.
Qt has really great docs, you should use them.
If someFunction doesn't modify or store pointer to in data you can use this:
QByteArray inArray = " ... ";
unsigned char *out;
someFunction((unsigned char*)(inArray.data()), out);
QByteArray outArray((char*)out);
Otherwise you have to make a deep copy of the char* returned by QByteArray::data() (see the docs for code snippet).
if someFunction takes a const char* args then just use ConstData() or data() in QByteArray class.
if you need a char*, you can then use strdup(). This method is doing this
char *strdup (const char *s) {
char *d = malloc (strlen (s) + 1); // Space for length plus nul
if (d == NULL) return NULL; // No memory
strcpy (d,s); // Copy the characters
return d; // Return the new string
}
more info here: strdup() - what does it do in C?

Resources