T
termin
consider this
char *arr[5];
why doesn't
arr=malloc(20);
this work ?
char *arr[5];
why doesn't
arr=malloc(20);
this work ?
consider this
char *arr[5];
why doesn't
arr=malloc(20);
this work ?
termin said:consider this
char *arr[5];
why doesn't
arr=malloc(20);
this work ?
DevarajA said:termin ha scritto:Because arr is already declared as a char* array and the name of anconsider this
char *arr[5];
why doesn't
arr=malloc(20);
this work ?
array becomes a const pointer to the first element of the array.
DevarajA wrote:
termin ha scritto:
consider this
char *arr[5];
why doesn't
arr=malloc(20);
this work ?
Because arr is already declared as a char* array and the name of an
array becomes a const pointer to the first element of the array.
No, /it does not/.
The types are different, as seventeen seconds with `sizeof`
and your favourite compiler - and other experiments - will
support.
DevarajA said:Chris Dollin ha scritto:DevarajA wrote:
termin ha scritto:
consider this
char *arr[5];
why doesn't
arr=malloc(20);
this work ?
Because arr is already declared as a char* array and the name of an
array becomes a const pointer to the first element of the array.
No, /it does not/.
The types are different, as seventeen seconds with `sizeof`
and your favourite compiler - and other experiments - will
support.
It doesnt when you use sizeof, initialize with a string literal or use
operator &. In other cases it does. Anyway you're right, it would have
been better to say "it acts like a pointer".
termin said:consider this
char *arr[5];
why doesn't
arr=malloc(20);
this work ?
termin said:consider this
char *arr[5];
why doesn't
arr=malloc(20);
this work ?
Chris said:DevarajA said:Chris Dollin ha scritto:DevarajA wrote:
termin ha scritto:
consider this
char *arr[5];
why doesn't
arr=malloc(20);
this work ?
Because arr is already declared as a char* array and the name of an
array becomes a const pointer to the first element of the array.
No, it would have been better to say that it's an array, and
arrays aren't assignable in C.
Pretending it's a pointer only defers the pain, and gives it
a chance to grow.
consider this
char *arr[5];
why doesn't
arr=malloc(20);
this work ?
Because arr is a 5-element array of pointer to char, not a pointer to a
5-element array of char, and you cannot write to an array object. If
your intent was to create an array of 5 20-character strings, the
general procedure is
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *arr[5];
int i;
for (i = 0; i < sizeof arr / sizeof arr[0]; i++)
{
arr = malloc(sizeof arr * 20);
if (arr)
{
strcpy(arr, "initial string");
}
}
/* do something interesting */
return 0;
}
Chris Dollin ha scritto:DevarajA wrote:
termin ha scritto:
consider this
char *arr[5];
why doesn't
arr=malloc(20);
this work ?
Because arr is already declared as a char* array and the name of an
array becomes a const pointer to the first element of the array.
It doesnt when you use sizeof, initialize with a string literal or use
operator &.
In other cases it does.
Anyway you're right, it would have
been better to say "it acts like a pointer".
ozbear said:consider this
char *arr[5];
why doesn't
arr=malloc(20);
this work ?
Because arr is a 5-element array of pointer to char, not a pointer to a
5-element array of char, and you cannot write to an array object. If
your intent was to create an array of 5 20-character strings, the
general procedure is
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *arr[5];
int i;
for (i = 0; i < sizeof arr / sizeof arr[0]; i++)
{
arr = malloc(sizeof arr * 20);
if (arr)
{
strcpy(arr, "initial string");
}
}
/* do something interesting */
return 0;
}
<snip>
I believe the malloc call
arr = malloc(sizeof arr * 20);
should have been
arr = malloc(sizeof arr[0] * 20);
or, probably better
arr = malloc(20);
The original overallocates by a factor of sizeof(arr) since
arr is a char pointer, not a char.
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.