By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,478 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,478 IT Pros & Developers. It's quick & easy.

Pointer Allocation

P: n/a
Consider the following code:

char *type[200];
char buf[1];
..............
read(handle, buf, 1);
*type[counter++]=buf[0];

And what I need is to put the buf into the type[counter] but it thrown
an cannot read memory exception..

What I would like to have in memory is:
type[0]="text1";
type[1]="text2";

I need some way of inline arithmetics to do it?

Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

Pacher R. Dragos wrote:
Consider the following code:

char *type[200];
char buf[1];
..............
read(handle, buf, 1);
*type[counter++]=buf[0];

And what I need is to put the buf into the type[counter] but it thrown
an cannot read memory exception..

What I would like to have in memory is:
type[0]="text1";
type[1]="text2";

I need some way of inline arithmetics to do it?

type is an array of pointer to character arrays.
buff is a character array;

size of buff should be increased
e.g.
char buff[MAX_STRING_SIZE];

after this put this inside a loop

{
buff = update_string();
type[i]=buff;

}

will serve the purpose

Nov 15 '05 #2

P: n/a
> char buff[MAX_STRING_SIZE];

If I do that i have to change hundrets of lines of code,
even if the code it's very modular

I intentionatelly do it like that!

after this put this inside a loop

{
buff = update_string();
type[i]=buff;

}

will serve the purpose


You didn't understand me:
for *type[counter]=buf[i] inside a loop
and inside another one I increase the counter

But I can't add succesive values to *type[0] so I need some kind of
pointer arithmetics:

Check for the word here:

while( i < 100 )
{
read(dhandle, tbuf, 1);
i++;
header[i] = tbuf[0];
/*here!!!*/ //++(++(*type[indice])) = tbuf[0]; /*
atentie posibile erori obscure */
if (tbuf[0]=='#')
{
//printf("_found end_\n"); /* DEBUG ### */

/* new search for keywords */

int local = 0;
indice++;

while ( local < 200 )
{
read(dhandle, tbuf, 1);
if ( tbuf[0] == '~' )
break;
}

if ( /**/ == /**/ )
{ //printf("_keyword_start_"); /* DEBUG */
do
{
read(dhandle, tbuf, 1);
source[k] = tbuf[0];
k++;
}
while( /**/ != /**/ );

//printf("_curent_source_%s_", source); /* DEBUG
*/
/*source[k-2]='&';*/ /* tre sa fac ceva cu
bufferul ala prost */

Nov 15 '05 #3

P: n/a
char* type[200];
yes , there are 200 pointers , but which address do they point to ?
so you have to figure out them before you read the memory.

Nov 15 '05 #4

P: n/a

vire wrote:
char* type[200];
yes , there are 200 pointers , but which address do they point to ?
so you have to figure out them before you read the memory.


They just hold data like

char *pointer[2]={"text one", "another hot text", "bla bla"};

So i must fill them with my special searched words.

Maybe a malloc will work ? ?? hmmm

Nov 15 '05 #5

P: n/a
On 26 Oct 2005 00:02:42 -0700, "Pacher R. Dragos"
<re**********@gmail.com> wrote:
Consider the following code:

char *type[200];
char buf[1];
..............
read(handle, buf, 1);
*type[counter++]=buf[0];
type is an array of 200 pointers, none of which point any where.

type[counter++] is one of those pointers which has not been
initialized to point somewhere.

*type[counter++] attempts to dereference this uninitialized pointer
which invokes undefined behavior.
And what I need is to put the buf into the type[counter] but it thrown
an cannot read memory exception..

What I would like to have in memory is:
type[0]="text1";
type[1]="text2";

I need some way of inline arithmetics to do it?


type[counter] = malloc(amount of storage necessary to hold string);
strcpy(type[counter++], buf); /* or maybe strncpy */
<<Remove the del for email>>
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.