* Dinamic array allocation
@ 2004-10-26 3:03 Fabio
2004-10-26 4:09 ` Ron Michael Khu
2004-10-26 5:51 ` Glynn Clements
0 siblings, 2 replies; 6+ messages in thread
From: Fabio @ 2004-10-26 3:03 UTC (permalink / raw)
To: linux-c-programming
Hi.
I am programming a C code that gets argv[1] as the lenght of an array
that is the copy of an array plus more data.
So, the appication runs as: ./a.out 100
It create an array of 100 bytes and adds a static defined array plus
more data, like:
char *foo[5]="bar"
main(int argc, char *argv[])
{
char *Ptr[];
Ptr = malloc( argv[1]+strlen(foo));
....
}
Makes sense? guess no.
Any suggestion is welcome.
thanks in advance
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Dinamic array allocation
2004-10-26 3:03 Dinamic array allocation Fabio
@ 2004-10-26 4:09 ` Ron Michael Khu
2004-10-26 4:12 ` Ron Michael Khu
2004-10-26 4:59 ` Lejanson C. Go
2004-10-26 5:51 ` Glynn Clements
1 sibling, 2 replies; 6+ messages in thread
From: Ron Michael Khu @ 2004-10-26 4:09 UTC (permalink / raw)
To: Fabio; +Cc: linux-c-programming
arvp[1] is string....
u should convert it to a numeric type via atoi() or strtol()....
=)
Fabio wrote:
> Hi.
>
> I am programming a C code that gets argv[1] as the lenght of an array
> that is the copy of an array plus more data.
>
> So, the appication runs as: ./a.out 100
>
> It create an array of 100 bytes and adds a static defined array plus
> more data, like:
>
> char *foo[5]="bar"
> main(int argc, char *argv[])
> {
> char *Ptr[];
> Ptr = malloc( argv[1]+strlen(foo));
> ....
> }
>
> Makes sense? guess no.
>
> Any suggestion is welcome.
>
> thanks in advance
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Dinamic array allocation
2004-10-26 4:09 ` Ron Michael Khu
@ 2004-10-26 4:12 ` Ron Michael Khu
2004-10-26 4:59 ` Lejanson C. Go
1 sibling, 0 replies; 6+ messages in thread
From: Ron Michael Khu @ 2004-10-26 4:12 UTC (permalink / raw)
Cc: Fabio, linux-c-programming
oops.. i meant argv[1] not arvp[1]
sorry.
ex:
malloc( atoi( argv[1] ) + strlen( foo) );
Ron Michael Khu wrote:
> arvp[1] is string....
> u should convert it to a numeric type via atoi() or strtol()....
>
> =)
>
> Fabio wrote:
>
>> Hi.
>>
>> I am programming a C code that gets argv[1] as the lenght of an array
>> that is the copy of an array plus more data.
>>
>> So, the appication runs as: ./a.out 100
>>
>> It create an array of 100 bytes and adds a static defined array plus
>> more data, like:
>>
>> char *foo[5]="bar"
>> main(int argc, char *argv[])
>> {
>> char *Ptr[];
>> Ptr = malloc( argv[1]+strlen(foo));
>> ....
>> }
>>
>> Makes sense? guess no.
>>
>> Any suggestion is welcome.
>>
>> thanks in advance
>> -
>> To unsubscribe from this list: send the line "unsubscribe
>> linux-c-programming" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at http://vger.kernel.org/majordomo-info.html
>>
>>
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Dinamic array allocation
2004-10-26 4:09 ` Ron Michael Khu
2004-10-26 4:12 ` Ron Michael Khu
@ 2004-10-26 4:59 ` Lejanson C. Go
2004-10-26 5:15 ` Ron Michael Khu
1 sibling, 1 reply; 6+ messages in thread
From: Lejanson C. Go @ 2004-10-26 4:59 UTC (permalink / raw)
To: Ron Michael Khu; +Cc: Fabio, linux-c-programming
I hope i am correct. :D
ptr = (char *) malloc (strtol(argv[1])+strlen(foo));
Ron Michael Khu wrote:
> arvp[1] is string....
> u should convert it to a numeric type via atoi() or strtol()....
>
> =)
>
> Fabio wrote:
>
>> Hi.
>>
>> I am programming a C code that gets argv[1] as the lenght of an array
>> that is the copy of an array plus more data.
>>
>> So, the appication runs as: ./a.out 100
>>
>> It create an array of 100 bytes and adds a static defined array plus
>> more data, like:
>>
>> char *foo[5]="bar"
>> main(int argc, char *argv[])
>> {
>> char *Ptr[];
>> Ptr = malloc( argv[1]+strlen(foo));
>> ....
>> }
>>
>> Makes sense? guess no.
>>
>> Any suggestion is welcome.
>>
>> thanks in advance
>> -
>> To unsubscribe from this list: send the line "unsubscribe
>> linux-c-programming" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at http://vger.kernel.org/majordomo-info.html
>>
>>
>
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Dinamic array allocation
2004-10-26 4:59 ` Lejanson C. Go
@ 2004-10-26 5:15 ` Ron Michael Khu
0 siblings, 0 replies; 6+ messages in thread
From: Ron Michael Khu @ 2004-10-26 5:15 UTC (permalink / raw)
Cc: Lejanson Go, Fabio, linux-c-programming
This is Mr. Fabio's code:
main(int argc, char *argv[])
{
char *Array[];
int length,i;
char foo[10]="bar";
length = strlen(foo) + atoi(argv[1]);
Array = malloc(length)
for(i=;i<length;i++)
Array[i]="a";
}
Fabio, ur trying to create an array of char ptrs.....
so u need to modify the way u mallocs space...
Array = malloc(length)
is only applicable if u have defined Array
as char *Array;
(and u need to initialize "i" just in case... im not
sure about the default value of ints)
Lejanson C. Go wrote:
> I hope i am correct. :D
>
> ptr = (char *) malloc (strtol(argv[1])+strlen(foo));
>
>
> Ron Michael Khu wrote:
>
>> arvp[1] is string....
>> u should convert it to a numeric type via atoi() or strtol()....
>>
>> =)
>>
>> Fabio wrote:
>>
>>> Hi.
>>>
>>> I am programming a C code that gets argv[1] as the lenght of an
>>> array that is the copy of an array plus more data.
>>>
>>> So, the appication runs as: ./a.out 100
>>>
>>> It create an array of 100 bytes and adds a static defined array plus
>>> more data, like:
>>>
>>> char *foo[5]="bar"
>>> main(int argc, char *argv[])
>>> {
>>> char *Ptr[];
>>> Ptr = malloc( argv[1]+strlen(foo));
>>> ....
>>> }
>>>
>>> Makes sense? guess no.
>>>
>>> Any suggestion is welcome.
>>>
>>> thanks in advance
>>> -
>>> To unsubscribe from this list: send the line "unsubscribe
>>> linux-c-programming" in
>>> the body of a message to majordomo@vger.kernel.org
>>> More majordomo info at http://vger.kernel.org/majordomo-info.html
>>>
>>>
>>
>> -
>> To unsubscribe from this list: send the line "unsubscribe
>> linux-c-programming" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at http://vger.kernel.org/majordomo-info.html
>>
>>
>
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Dinamic array allocation
2004-10-26 3:03 Dinamic array allocation Fabio
2004-10-26 4:09 ` Ron Michael Khu
@ 2004-10-26 5:51 ` Glynn Clements
1 sibling, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2004-10-26 5:51 UTC (permalink / raw)
To: Fabio; +Cc: linux-c-programming
Fabio wrote:
> I am programming a C code that gets argv[1] as the lenght of an array
> that is the copy of an array plus more data.
>
> So, the appication runs as: ./a.out 100
>
> It create an array of 100 bytes and adds a static defined array plus
> more data, like:
>
> char *foo[5]="bar"
This should be:
const char *foo = "bar";
or:
char foo[] = "bar";
or:
char foo[5] = "bar";
Exactly which one depends upon exactly what you want.
In the first case, the string will be stored in the read-only data
segment, the variable foo will be stored in the data segment, and foo
will be initialised to point to the string. You will be able to modify
the variable foo to point elsewhere, e.g.
foo = "foo";
but you won't be able to modify what it points to (because the
read-only data segment is mapped read-only), i.e. doing:
foo[0] = 'x';
will result in a segmentation fault at run-time.
In the second and third cases, foo will effectively be a compile-time
constant which points to a region of the data segment (to at least 4
bytes in the second case, at least 5 bytes in the third case). You
will be able to modify the contents of the array, i.e.
foo[0] = 'x';
but you won't be able to make foo point elsewhere, i.e. doing:
foo = "foo";
will result in an error at compile time.
> main(int argc, char *argv[])
> {
> char *Ptr[];
This should probably just be:
char *Ptr;
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2004-10-26 5:51 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-10-26 3:03 Dinamic array allocation Fabio
2004-10-26 4:09 ` Ron Michael Khu
2004-10-26 4:12 ` Ron Michael Khu
2004-10-26 4:59 ` Lejanson C. Go
2004-10-26 5:15 ` Ron Michael Khu
2004-10-26 5:51 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).