Pointer List How?

B

Bryan Parkoff

I tried to create pointer array. Each element in this array has 4 bytes
or pointer. How can I create pointer array instead long (dword) array? I
do not have to convert from "unsigned long" to "char*".
It should look like *A[0], *A[1], *A[2].....
If it is not the option what C/C++ Compiler has, I will be forced to use
struct keyword. If I have to use struct array that would be 100 structs in
one array, how can struct be initialized at dynamic memory instead of static
memory using new keyword?
Please advise and look at my code below.

char *name1 = "Bryan";
char *name2 = "Jon";
char *name3 = "Daniel";

unsigned long* A = new unsigned long[0x1000];

A[0] = (unsigned long)name1;
A[1] = (unsigned long)name2;
A[2] = (unsigned long)name3;

char *name4 = (char*)A[0];

Bryan Parkoff
 
M

Matt Bitten

Bryan said:
I tried to create pointer array.

There are two answers to your question.

First, combining the syntax for arrays and pointers in C
can be done, but is error prone. Use a typedef to make it
easier.

typedef char * my_ptr_type;
my_ptr_type array1[100];
my_ptr_type * dynamic_array = new my_ptr_type[0x1000];

Now for the second answer. What you really seem to be
asking is how to create an array of strings. If you are stuck
with plain C, then the above solution is the way to do it;
however, you are asking on a C++ group. So the real solution
is to use ... an array of strings.

#include <string>
using std::string;

string array1[100];
string * dynamic_array = new string[0x1000];
 
B

Bryan Parkoff

Matt,

I want to say thank you very much for the answer. I believe that first
option is useful because string in second option uses signed char, but not
unsigned char. It is what I am using unsigned char. You may notice signed
char in my code below which it is just an example.
I always use C keyword in C++ source code because C++ source code is
very flexible than C source code. It is like variables are forced to be in
the top of each function in C source code, but C++ source code does not. I
will try in my code later.

Bryan Parkoff

Matt Bitten said:
Bryan said:
I tried to create pointer array.

There are two answers to your question.

First, combining the syntax for arrays and pointers in C
can be done, but is error prone. Use a typedef to make it
easier.

typedef char * my_ptr_type;
my_ptr_type array1[100];
my_ptr_type * dynamic_array = new my_ptr_type[0x1000];

Now for the second answer. What you really seem to be
asking is how to create an array of strings. If you are stuck
with plain C, then the above solution is the way to do it;
however, you are asking on a C++ group. So the real solution
is to use ... an array of strings.

#include <string>
using std::string;

string array1[100];
string * dynamic_array = new string[0x1000];
 
D

DHOLLINGSWORTH2

in your code you say you want a pointer array :

unsigned long* A = new unsigned long[0x1000];

but in these lines:
A[0] = (unsigned long)name1;
A[1] = (unsigned long)name2;
A[2] = (unsigned long)name3;

you are storing them as Unsigned long integers;
not Pointers to Unsigned Long Integers.

I am interested in what error msg you get. It looks like your code would
work fine like :

A[0] = (unsigned long *)name1;
A[1] = (unsigned long *)name2;
A[2] = (unsigned long *)name3;

give it a try.




Bryan Parkoff said:
Matt,

I want to say thank you very much for the answer. I believe that first
option is useful because string in second option uses signed char, but not
unsigned char. It is what I am using unsigned char. You may notice
signed char in my code below which it is just an example.
I always use C keyword in C++ source code because C++ source code is
very flexible than C source code. It is like variables are forced to be
in the top of each function in C source code, but C++ source code does
not. I will try in my code later.

Bryan Parkoff

Matt Bitten said:
Bryan said:
I tried to create pointer array.

There are two answers to your question.

First, combining the syntax for arrays and pointers in C
can be done, but is error prone. Use a typedef to make it
easier.

typedef char * my_ptr_type;
my_ptr_type array1[100];
my_ptr_type * dynamic_array = new my_ptr_type[0x1000];

Now for the second answer. What you really seem to be
asking is how to create an array of strings. If you are stuck
with plain C, then the above solution is the way to do it;
however, you are asking on a C++ group. So the real solution
is to use ... an array of strings.

#include <string>
using std::string;

string array1[100];
string * dynamic_array = new string[0x1000];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top