LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-02-2004, 09:11 AM   #1
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
Question Arrays in C++


hi,

since I know java and pascal I have som problem with pointers.

In C++ It seams that I get a pointer and not memory referance when using array. With a cheracter I do not need a * (pointer ref) but for the array I need it since I get a invalid conversion from `char*' to `char'.


This prog compiles (gcc)

void function1(char chr);

main ...
{
char chr = "";
function1(chr);
}

void function1(char chr)
{}

This one as well

void function1(char * arr);

main ...
{
char array1[10] = "123456789";
function1(array1);
}

void function1(char * arr)
{}

This one givs error invalid conversion from `char*' to `char `

void function1(char arr);

main ...
{
char array1[10] = "123456789";
function1(array1);
}

void function1(char arr)
{}


SO I think the syntax isn't clear. Does someone have an explication why its like this and not made with an pointer ref like: char * array1[10] = "123456789"; How do I get the memory alocation without a pointer? If its possible.


I love the vector class i java is it some thing like it in C++?
 
Old 02-02-2004, 09:33 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
I will try and explain this in terms of what C does for the following declarations:

char ch; This sets a side the amount of space for 1 character, exactly what you would expect.

char array[10]; This creates a pointer to a char (char *) called array. It points to a statically allocated block of 10 characters. Array is a pointer to the address where this block of memory starts.

So say this is your memory space being mapped by array[10] = "123456789":

Address Value
0x0010 1
0x0011 2
0x0012 3
....
0x0019 4

All array is is a pointer to 0x0010. To get the value at that address you would type *array, or array[0]. Typing array[x] is equivalent to typing *(array + x).

You can't allocate memory without a pointer in C. Pointers are very powerful, but take some getting used to. If you want to look into dynamically allocating memory you would do something like this:

char *array = malloc(sizeof(char) * 10); //points array to a block of memory that can hold 10 char.

array = realloc(sizeof(char)* 20); //reallocates array to point to a block of 20 characters

free(array); //releases the memory pointed to by array.

You also probably want to check out the memcpy command.

One thing you have to be really careful is your bounds, C will let you go beyond array bounds without complaining at compile time. However, those kind of situations often cause unusual things happening in your program or the dreaded words "segmentation fault".
 
Old 02-03-2004, 10:58 AM   #3
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
Thanks for the expanation. I got it now and its just to accept the notation. Even if I think its clerar if it would be
char * array[10] = "123456789";
 
Old 02-03-2004, 11:01 AM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Ah, but what you have written there is a pointer to a pointer that points to a static block of memory big enough for 10 characters

blah[] and *blah are equivlent
so *blah[] and **blah and blah[][] are equivlent.

Last edited by jtshaw; 02-03-2004 at 11:08 AM.
 
Old 02-06-2004, 06:02 AM   #5
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
Well I been thinking about this and then I found some info in kdevelop under GCC compiler error messages. To add the [] in method head gives a nice syntax.


warning: passing arg 1 of `cpystr' makes integer from pointer
without a cast

This is the code causing the problem:
void cpystr( char item);
main()
{
char src[]="martin leslie";
cpystr(src);
}
cpystr(char item)
{
}

It should be....
void cpystr( char item[]);
main()
{
char src[]="martin leslie";
cpystr(src);
}
cpystr(char item[])
{
}
 
Old 02-07-2004, 12:27 AM   #6
schmack
LQ Newbie
 
Registered: Feb 2004
Posts: 12

Rep: Reputation: 0
Actually jtshaw got it a little wrong.

When you have:

char arr[10];

It actually allocates 10 bytes. No pointer is allocated.

When an array is passed as a paramter to a function, the
address of the first character is sent to the function. This
is done so the C language doesn't have to track sizes of
arrays and copy all the bytes onto the stack for a function
call.

Also, people rarely use the [] to denote arrays being passed to
a function. You'll see (and people will generally expect) *
in most cases.

Good luck!

-Craig
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using associative arrays corbis_demon Programming 0 01-25-2005 12:47 AM
help with arrays leroy27336 Programming 6 01-20-2004 06:07 PM
Arrays and Letters? Gerardoj Programming 1 09-24-2003 12:25 PM
arrays in perl BBQ_Matt Programming 3 09-23-2003 07:45 AM
C. Arrays and Letters? Gerardoj Programming 1 09-22-2003 01:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration