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.
Hey all,
Gotta love the confusing world of pointers.
How does one know when to use a pointer or just a variable??
for example char some_name or char *some_name???
I have a problem reference a function that returns a pointer (dont even know if it should or not)
I have created a data structure class: Item
In my main program, i have made a global variable: (An array)
i am tryin to insert into the Item array, at each position a number (and other data members of Item). WHen i try to call:
item[some_index].getNumber();
I only get the first char of the string??
can anyone throw some ideas at me... thank you so much
I am still a lil premature in my code, but i think i am also having trouble referencing different indicies in my array, is this declared properly?? thanks again
Short example showing what is wrong with your code:
Code:
#include <iostream>
using namespace std;
int main(void){
char blah[]= "abcd";
char blah2[]="something";
*blah2 = * blah;
cout<<blah<<" "<<blah2<<endl;
}
The result is
Code:
abcd aomething
That's because *blah2 = *blah copies only one byte. It's equivalent to blah2[0] = blah[0]. If you want to copy strings, use strcpy function (or C++ equivalents).
someone can correct me if i am wrong but variables get passed to functions in C and C++ by reference so the function is only working with a copy of the variable to be able to do it's function, it doesn't alter the variable at all and if you want it to do so you'd need to pass a pointer that points to the variables address to have the function actually change the vaule of the variable. hope that doesn't sound too confusing. that is one reason to use pointers. Also arrayname[ ] is really a pointer to the first element so you can have a pointer piont to that to access the array elements .
Last edited by dr_zayus69; 07-06-2005 at 06:21 AM.
did i at least have the concept right if the terminally wrong? The value is just a copy so the variable doesn't get changed and you'd need to pass by reference to change the actual variable. It's been a little time since i read about pointers but that fact stuck out to me cuz when i read about them i didn't know what the point of using them was so i looked into it. lol
Originally posted by dr_zayus69 did i at least have the concept right if the terminally wrong? The value is just a copy so the variable doesn't get
changed and you'd need to pass by reference to change the actual variable.
If you want to copy strings, use strcpy function (or C++ equivalents).
Also, always make sure you have enough space for the copied string; coincidentally in this case blah2 is longer than blah, but if you try the other way around you should get a segmentation fault because you'd be writing past the end of the array. When you use [] = "" then you are implicitly creating an array the size of whatever is in the "", therefore the size of blah is fixed at 4 characters, and blah2 is fixed at 9. There is no changing the size once it's declared, so always check the size of what you are copying with strlen first and make sure where you are copying it to is big enough.
ta0kira
quote:
someone can correct me if i am wrong but variables get passed to functions in C and C++ by reference
It is pass by Value. Pass by reference is wen u send the pointer.
That is not exactly correct... In "C" all non-array (character, or numeric) variables default to getting passed by value while all array (character, or numeric) variables are always passed by reference. The original K&R specification had the compiler attempting to pass all arguments on the stack as single cpu-sized words where each word represents one variable. In C++ variables are passed by value as the default, but the programmer has the option of specifying that a variable is to be passed by reference by using the ampersand "&" sign in the function specification/declaration
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.