Dinamic array allocation
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
arvp[1] is string....
u should convert it to a numeric type via atoi() or strtol()....
=)
Quoted 27 lines · Fabio
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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
oops.. i meant argv[1] not arvp[1]
sorry.
ex:malloc( atoi( argv[1] ) + strlen( foo) );
Quoted 36 lines · Ron Michael Khu
arvp[1] is string....
u should convert it to a numeric type viaatoi()orstrtol()....=)
Quoted 27 lines · Fabio
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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
I hope i am correct. :D
ptr = (char *) malloc (strtol(argv[1])+strlen(foo));
Quoted 41 lines · Ron Michael Khu
arvp[1] is string....
u should convert it to a numeric type viaatoi()orstrtol()....=)
Quoted 18 lines · Fabio
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.
Quoted 9 lines
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 [email protected]
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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
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)
Quoted 51 lines · Lejanson C. Go
I hope i am correct. :D
ptr = (char *) malloc (strtol(argv[1])+strlen(foo));
Quoted 41 lines · Ron Michael Khu
arvp[1] is string....
u should convert it to a numeric type viaatoi()orstrtol()....=)
Quoted 18 lines · Fabio
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.
Quoted 9 lines
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 [email protected]
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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 9 lines · Fabio
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.
Quoted 3 lines
main(int argc, char *argv[]){ char *Ptr[];
This should probably just be:
char *Ptr;
Signature · 1 line
Glynn Clements <[email protected]>