LinuxQuestions.org
Review your favorite Linux distribution.
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 07-05-2005, 11:09 AM   #1
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Rep: Reputation: 30
to point or not to point??????


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)

Code:
Item *item = new Item[10];
I have these functions in Item.cc:

Code:
void Item::setNumber(char *num)
{
    Inumber = new char[sizeof(num)];
    *Inumber=*num;
    //cout << "setNumber function, Inumber: " << Inumber << endl;
}

char *Item::getNumber()
{
    return Inumber;
}
I have this in Item.h:
(not all code included)

Code:
public:
void setNumber(char *num);
char *getNumber();

private: 
char *Inumber;
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

Josh
 
Old 07-05-2005, 05:13 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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).
 
Old 07-05-2005, 08:00 PM   #3
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Original Poster
Rep: Reputation: 30
wow, yea strcpy worked like a champ.

I also got rid of my pointers, and just used the char array.

THank you very much!!!
 
Old 07-06-2005, 06:17 AM   #4
dr_zayus69
Member
 
Registered: Sep 2004
Location: western massachusetts
Distribution: fedora core 3, Suse 10
Posts: 877

Rep: Reputation: 35
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.
 
Old 07-06-2005, 06:30 AM   #5
hk_linux
Member
 
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
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.
 
Old 07-06-2005, 06:45 AM   #6
dr_zayus69
Member
 
Registered: Sep 2004
Location: western massachusetts
Distribution: fedora core 3, Suse 10
Posts: 877

Rep: Reputation: 35
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
 
Old 07-06-2005, 06:50 AM   #7
hk_linux
Member
 
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
Quote:
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.
Exactly.
 
Old 07-11-2005, 03:09 AM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
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
 
Old 07-11-2005, 03:54 AM   #9
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Hmmm i was looking at this thread and this caught my eye.
Code:
#include <iostream>

using namespace std;

int main(void){
  char blah[]= "abcd";
  char blah2[]="something";
  *blah2 = * blah;
  cout<<blah<<" "<<blah2<<endl;
}
yikes i would expect some strange results with that one there. Garbage really would be the only thing you would get.
 
Old 07-11-2005, 12:23 PM   #10
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Quote:
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
 
  


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
What is WAN point-to-point link? ivj Linux - Networking 1 07-16-2005 01:20 AM
Point to point connectivity between 2 pcs shroffsp Linux - Networking 1 09-22-2003 08:57 AM
Mandrake MNF with point-to-point T1, routers ioannes Linux - Networking 0 07-24-2003 08:59 AM
Is there a detailed point by point comparison on Linux to Windows? Paul Parr Linux - General 4 04-26-2003 02:35 AM
point to point address assignment of ppp0 andyn Linux - Networking 0 10-11-2002 10:45 PM

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

All times are GMT -5. The time now is 01:01 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