473,395 Members | 1,978 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,395 software developers and data experts.

2d arrays!

#include <stdio.h>

int main(int argc, char **argv)
{

char i[][10] = {"welcome", "nelcome"};
// pointer to the array of chars
char (*p)[] = {"Welcoem", "welcome"}; //dosent work

// array of pointer to chars
char *s[] = {"welcome", "welcome"}; // works strange!
return 0;
}

Aug 21 '08 #1
7 1375
On Aug 21, 8:53 am, raashid bhatt <raashidbh...@gmail.comwrote:
#include <stdio.h>

int main(int argc, char **argv)
{

char i[][10] = {"welcome", "nelcome"};
// pointer to the array of chars
char (*p)[] = {"Welcoem", "welcome"}; //dosent work
p is a pointer to an array; as such, it is expecting an array address,
not 2-d array initializer.

What you're looking for is something like this:

char (*p)[] = &"Welcome"; // note the presence of the & operator

Alternately, something like this:

char a[] = "Welcome";
char (*p)[] = &a;
// array of pointer to chars
char *s[] = {"welcome", "welcome"}; // works strange!
String literals are stored as arrays of char; if a string literal
appears anywhere other than as an operator to & or sizeof, the usual
array conversions come into play (i.e., the reference to the string is
converted to type char*, and the value of the reference is set to
point to the first character in the string).

So your initializer can be seen as two objects of type char*.
return 0;

}
You really, really, *REALLY* need to get a good C reference manual,
because you've been asking some very basic questions that are best
answered by such a reference. My main reference is Harbison &
Steele's "C: A Reference Manual" (currently 5th edition). Whatever
you get, go there *first*, and if something still doesn't make sense,
feel free to ask here.
Aug 21 '08 #2
feel free to ask here.
Thanks a lot Man at last u understood my prob
Aug 21 '08 #3
John Bode wrote:
On Aug 21, 8:53 am, raashid bhatt <raashidbh...@gmail.comwrote:
....
char (*p)[] = {"Welcoem", "welcome"}; //dosent work

p is a pointer to an array; as such, it is expecting an array address,
not 2-d array initializer.

What you're looking for is something like this:

char (*p)[] = &"Welcome"; // note the presence of the & operator
I doubt that he's looking for code which violates a constraint. The
operand of & must be an lvalue.
Aug 21 '08 #4
raashid bhatt wrote:
#include <stdio.h>

int main(int argc, char **argv)
{

char i[][10] = {"welcome", "nelcome"};
// pointer to the array of chars
char (*p)[] = {"Welcoem", "welcome"}; //dosent work

// array of pointer to chars
char *s[] = {"welcome", "welcome"}; // works strange!
return 0;
}
John Bode has already discussed why 'p' doesn't work. However, it
would still be helpful if you could show us what you want to do with
these objects.

I would strongly recommend declaring 's' as "const char*s[]", because
the memory its elements point at is memory you cannot safely write to.
Declaring it 'const' will make most attempts to mis-use it into errors
that the compiler can warn you about. So long as you don't need to
write to that memory, I'd recommend using 's'. Therefore, I'm curious
about why you think that it "works strange!". Could you give an
example of what's strange about the way it works?

If you need to be able to change the contents of the strings you're
working with, I'd recommend using 'i'. However, there are situations
where you might want to also define the following:

char *i_ptr[] = {&i[0], &i[1]};
Aug 21 '08 #5
On Thu, 21 Aug 2008 12:12:11 -0700, jameskuyper wrote:
John Bode wrote:
>What you're looking for is something like this:

char (*p)[] = &"Welcome"; // note the presence of the & operator

I doubt that he's looking for code which violates a constraint. The
operand of & must be an lvalue.
String literals are lvalues. 6.5.1p4: "A string literal is a primary
expression. It is an lvalue with type as detailed in 6.4.5."

Interestingly, at least one compiler agrees with you, though:

test.c:2: Error: Address-of operator applied to something that is not an
lvalue
char (*p)[] = &"Welcome";
^ here

I should probably go and report that as a bug.
Aug 21 '08 #6
Harald van D©¦k wrote:
On Thu, 21 Aug 2008 12:12:11 -0700, jameskuyper wrote:
John Bode wrote:
What you're looking for is something like this:

char (*p)[] = &"Welcome"; // note the presence of the & operator
I doubt that he's looking for code which violates a constraint. The
operand of & must be an lvalue.

String literals are lvalues. 6.5.1p4: "A string literal is a primary
expression. It is an lvalue with type as detailed in 6.4.5."

Interestingly, at least one compiler agrees with you, though:

test.c:2: Error: Address-of operator applied to something that is not an
lvalue
char (*p)[] = &"Welcome";
^ here
You're right - after drilling Raashid on the fact that there are three
situations in which lvalues with array type are not automatically
converted into pointers to the first element, I should have remembered
that string literals are the third exception. It's not an exception
that comes up very often, in my experience.
Aug 21 '08 #7
ja*********@verizon.net wrote:
Harald van D©¦k wrote:
>On Thu, 21 Aug 2008 12:12:11 -0700, jameskuyper wrote:
>>John Bode wrote:
What you're looking for is something like this:

char (*p)[] = &"Welcome"; // note the presence of the & operator
I doubt that he's looking for code which violates a constraint. The
operand of & must be an lvalue.
String literals are lvalues. 6.5.1p4: "A string literal is a primary
expression. It is an lvalue with type as detailed in 6.4.5."

Interestingly, at least one compiler agrees with you, though:

test.c:2: Error: Address-of operator applied to something that is not an
lvalue
char (*p)[] = &"Welcome";
^ here

You're right - after drilling Raashid on the fact that there are three
situations in which lvalues with array type are not automatically
converted into pointers to the first element, I should have remembered
that string literals are the third exception. It's not an exception
that comes up very often, in my experience.
You're wrong again.

It's the address operator that is suppressing the conversion.

The special rule for string literals,
is when they are initializers for an array,
as in:

char array[] = "Welcome";

--
pete
Aug 22 '08 #8

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

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.