Just wondering if someone can clear things up for me a little. I'm fairly new to C/C++ and just getting in a bit of a tangle with pointers.
My problem is I have a vector of a defined structure, which needs to store some characters. When I try and print out these characters later, it appears to be uninitialised. I believe the problem is because I'm using a char*, who's memory contents then just gets discarded later on, and hence I can't get the characters anymore.
e.g. -
typedef struct _info{
-
char* label;
-
} Info;
a little later on some parsing takes place... -
char *value;
-
-
for (....)
-
{
-
char *label;
-
Info info;
-
label = value;
-
info.label = label;
-
-
// info gets added to a vector
-
}
-
When I come to print info later I get ▌▌▌▌▌▌▌▌▌▌
Is my understanding of this correct? If so, then I guess that label within the Info structure needs to be a char[]. Is there a simple way to then copy the 'value' within the for loop into this char array as my attempts have just led to compiler errors... ?
Thanks very much
6 2064
I'm going to reply to my own question for the benefit of anyone else...
I think explaining the problem is right, and the useful little way to do this is to use strcpy... -
char dest[50];
-
char* pointer = "hello world";
-
-
strcpy(dest, pointer);
-
pointer can now be played around with freely without changing the value in dest.
A char* is a pointer to a single char.
A char[] is also a pointer to a char but you have to specify the char(s) or it won't compile: -
char arr[] ="ABC"; /* OK. arr is an array of 4 char */
-
char arr1[] = {'A', 'B', 'C'}; /* OK. arr1 is an array of 3 char */
-
char arr2[]; /* ERROR: No char(s) specified.
-
Read this:
First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
That is an array of 5 elements each of which is an int.
won't compile. You need to declare the number of elements.
Second, this array:
is still an array of 5 elements. Each element is an array of 10 int.
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.
won't compile. You need to declare the number of elements.
Third, the name of an array is the address of element 0
Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.
Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int: -
int array[5][10];
-
-
int (*ptr)[10] = array;
-
Fourth, when the number of elements is not known at compile time, you create the array dynamically: -
int* array = new int[value];
-
int (*ptr)[10] = new int[value][10];
-
int (*ptr)[10][15] = new int[value][10][15];
-
In each case value is the number of elements. Any other brackets only describe the elements.
Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code: -
int** ptr = new int[value][10]; //ERROR
-
new returns the address of an array of 10 int and that isn't the same as an int**.
Likewise: -
int*** ptr = new int[value][10][15]; //ERROR
-
new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.
With the above in mind this array: -
int array[10] = {0,1,2,3,4,5,6,7,8,9};
-
has a memory layout of
0 1 2 3 4 5 6 7 8 9
Wheras this array: -
int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
-
has a memory layout of
0 1 2 3 4 5 6 7 8 9
Kinda the same, right?
So if your disc file contains
0 1 2 3 4 5 6 7 8 9
Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.
Therefore, when you do your read use the address of array[0][0] and read as though you have a
one-dimensional array and the values will be in the correct locations.
In your post, you have said that
won't compile because we need to declare the number of elements.
But, if i do like this,
The compiler gives the error - declaration of ‘a’ as multidimensional array must have bounds for all dimensions except the first
Also, when i do like this,
int array[][10] = { { 0 } };
It gets compiled successfully.
Can you please explain what's happening?
Well, for the last used declariation with the zero you initialize the variable to be like this: -
int index[1][10] = { 0 };
-
Also you don't need a "duo" of curly brackets for that like you used... Anyway I think that weaknessforcats meant the following use: -
#include <stdio.h>
-
-
int Size(int index[][3]);
-
int main(int argc, char **argv)
-
{
-
// the first dimention (currently has a value of 1 can be any value
-
// the function will accept it, so it would be valid to write
-
// index[10][3]. the function would be ok with it!
-
int index[1][3];
-
-
Size(index);
-
return 0;
-
}
-
-
int Size(int index[][3])
-
{
-
return;
-
}
-
I hope this example helped you understand what's going on! Should you have any questions post!
In your post, you have said that
Code: ( text )
int array[][10];
won't compile because we need to declare the number of elements.
But, if i do like this,
Code: ( text )
int array[10][];
The compiler gives the error - declaration of ‘a’ as multidimensional array must have bounds for all dimensions except the first
Yep.
can't compile becuse you failed to specify the size of the elements.
int array[][10] = { { 0 } };
It gets compiled successfully.
Can you please explain what's happening?
You are using initialization syntax. This is the same as: -
int array[1][10] = { { 0 } };
-
Rememvber:
is the same as: -
int arr[5] = {1,2,3,4,5};
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Christopher Benson-Manica |
last post by:
I need a smart char * class, that acts like a char * in all cases, but
lets you do some std::string-type stuff with it. (Please don't say to
use std::string - it's not an option...). This is my...
|
by: Alex Vinokur |
last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c
> ben19777@hotmail.com (Ben) wrote:
> > 2) Structure casted into an...
|
by: Sona |
last post by:
I understand the problem I'm having but am not sure how to fix it. My
code passes two char* to a function which reads in some strings from a
file and copies the contents into the two char*s. Now...
|
by: Peter Nilsson |
last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think
about what the conversion of a char to unsigned char would mean, and whether
it was sensible to actually do so. And pete has...
|
by: jab3 |
last post by:
(again :))
Hello everyone.
I'll ask this even at risk of being accused of not researching adequately.
My question (before longer reasoning) is: How does declaring (or defining,
whatever) a...
| |
by: GRoll35 |
last post by:
I get 4 of those errors. in the same spot. I'll show my parent class,
child class, and my driver.
All that is suppose to happen is the user enters data and it uses
parent/child class to display...
|
by: Pedro Pinto |
last post by:
Hi there once more........
Instead of showing all the code my problem is simple.
I've tried to create this function:
char temp(char *string){
alterString(string);
return string;
|
by: Paul Brettschneider |
last post by:
Hello all,
consider the following code:
typedef char T;
class test {
T *data;
public:
void f(T, T, T);
void f2(T, T, T);
|
by: s0suk3 |
last post by:
This code
#include <stdio.h>
int main(void)
{
int hello = {'h', 'e', 'l', 'l', 'o'};
char *p = (void *) hello;
for (size_t i = 0; i < sizeof(hello); ++i) {
|
by: Kenzogio |
last post by:
Hi,
I have a struct "allmsg" and him member :
unsigned char card_number; //16
allmsg.card_number
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |