473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

pointer array problem

consider this

char *arr[5];

why doesn't

arr=malloc(20);

this work ?

Nov 15 '05 #1
11 1534
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. So it
can't be modified to point to other locations.

--
Devaraja (Xdevaraja87^gmail^c0mX)
Linux Registerd User #338167
http://counter.li.org
Nov 15 '05 #2
termin wrote:
consider this

char *arr[5];

why doesn't

arr=malloc(20);

this work ?


Because `arr` is an array, and you can't assign to an
entire array - only to elements of one.

--
Chris "electric hedgehog" Dollin
It's called *extreme* programming, not *stupid* programming.
Nov 15 '05 #3
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.

--
Chris "electric hedgehog" Dollin
It's called *extreme* programming, not *stupid* programming.
Nov 15 '05 #4
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".

--
Devaraja (Xdevaraja87^gmail^c0mX)
Linux Registerd User #338167
http://counter.li.org
Nov 15 '05 #5
DevarajA wrote:
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".


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.

--
Chris "electric hedgehog" Dollin
It's called *extreme* programming, not *stupid* programming.
Nov 15 '05 #6


termin wrote:
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[i] = malloc(sizeof arr[i] * 20);
if (arr[i])
{
strcpy(arr[i], "initial string");
}
}

/* do something interesting */
return 0;
}

If your intent was to create a single 20-element array of char, you
would do one of the following:

1. char *arr;
...
arr = malloc(20 * sizeof *arr);
if (arr)
{
strcpy(arr, "some string value");
}

2. char (*arr)[20];
...
arr = malloc(sizeof *arr); // note difference
if (arr)
{
strcpy(*arr, "some string value"); // note difference
}

The second method isn't as commonly used, mainly because it isn't as
flexible and introduces an extra level of indirection. I just included
it for completeness.

Remember:

char *arr[5]; -- arr is array of pointer to char
char (*arr)[5]; -- arr is pointer to array of char

Nov 15 '05 #7
termin wrote:
consider this
char *arr[5]; why doesn't
arr=malloc(20);
this work ?


arr is an array (of 5 pointers to char). It is not a pointer.
Nov 15 '05 #8
Chris Dollin wrote:
DevarajA wrote:
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.
Strictly speaking it becomes not a "const pointer" but a "non-lvalue
pointer".
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.
The string literal initialisation applies to the string literal itself,
not the array being initialised, which doesn't decay and remains an
array.
Anyway you're right, it would have
been better to say "it acts like a pointer".


Perhaps "it decays to a pointer".
No, it would have been better to say that it's an array, and
arrays aren't assignable in C.
For the reply to this specific post, you are right.
Pretending it's a pointer only defers the pain, and gives it
a chance to grow.


But not being aware that an array in most situations decays to a
pointer can become just as painful.

Nov 15 '05 #9
On 19 Jul 2005 09:07:30 -0700, "John Bode" <jo*******@my-deja.com>
wrote:


termin wrote:
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[i] = malloc(sizeof arr[i] * 20);
if (arr[i])
{
strcpy(arr[i], "initial string");
}
}

/* do something interesting */
return 0;
}

<snip>

I believe the malloc call
arr[i] = malloc(sizeof arr[i] * 20);
should have been
arr[i] = malloc(sizeof arr[i][0] * 20);
or, probably better
arr[i] = malloc(20);

The original overallocates by a factor of sizeof(arr[i]) since
arr[i] is a char pointer, not a char.

Oz
--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 15 '05 #10
On Tue, 19 Jul 2005 14:16:50 +0000, DevarajA wrote:
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.

arr is declared (and also defined) as an array of 5 pointers to char. It
is NOT declared as a char *.
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 those cases an array operand/initialiser is used directly, ...
In other cases it does.
.... in other cases it is converted to a pointer to the first element of
the array. A pointer to the first element of arr has type char **, not
char *.
Anyway you're right, it would have
been better to say "it acts like a pointer".


The significant point here is not that it evaluates to a pointer, it is
that it is not modifiable i.e. cannot appear on the LHS of an assignment
operator.

Lawrence
Nov 15 '05 #11


ozbear wrote:
On 19 Jul 2005 09:07:30 -0700, "John Bode" <jo*******@my-deja.com>
wrote:


termin wrote:
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[i] = malloc(sizeof arr[i] * 20);
if (arr[i])
{
strcpy(arr[i], "initial string");
}
}

/* do something interesting */
return 0;
}

<snip>

I believe the malloc call
arr[i] = malloc(sizeof arr[i] * 20);
should have been
arr[i] = malloc(sizeof arr[i][0] * 20);
or, probably better
arr[i] = malloc(20);

The original overallocates by a factor of sizeof(arr[i]) since
arr[i] is a char pointer, not a char.


Yeah, typo. That was supposed to read

arr[i] = malloc(sizeof *arr[i] * 20);

I make it a habit to include the sizeof for the target, rather than
just a raw number of bytes; for one thing, I think it makes the intent
of the code clearer (I'm allocating N elements of M size, rather than X
bytes), and for another, I'm usually allocating arrays of something
other than char, so I have to include the sizeof anyway.

Nov 15 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: xuatla | last post by:
I encountered "segmentation fault" and I checked my code, found the following problem: I want to reallocate memory for an array. I defined the following function: int reallocateMemory( double...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
6
by: Irrwahn Grausewitz | last post by:
Hey, y'all. While doing some pointer experiments I encountered a problem. I know that I know the answer already, but trying to remember I just screwed up my mind. I wonder if someone would be...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
2
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
8
by: Sam | last post by:
I have a situation occuring in my code and I just can't see to figure out why I have an structure called employee that will put all of the employee id's into a char array set to 10 struct...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
3
by: .rhavin grobert | last post by:
this is m$ vc++, but the question itself is c++ specific... TCHAR is a 'char' in non-unicode. functions return... TCHAR* CString::GetBuffer(int); int CString::GetLenght(int);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.