ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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++?
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":
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".
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[])
{
}
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.