LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-30-2008, 12:56 PM   #1
ichigo@tea
LQ Newbie
 
Registered: Sep 2008
Location: Michigan
Distribution: Zenwalk 5.2
Posts: 13

Rep: Reputation: 0
New to C++, some pointers are needed.


Now, I'm not a complete newbie. I know what classes are, and I know what they do. I know the basics of OOP. but that's about it. I just have some questions I'd like to ask from people, and not a book.


First of all: I have a problem with my Geany IDE.

As you can see in this: http://img296.imageshack.us/img296/7109/geanygz8.png There's a backspace pwn. I'm using:

Code:
#include <iostream>

using namespace std;
int main()
{

	
	
	
	cout << "Enter your mpg: ";
	cin >> mpg;
}
I was wondering how to fix that one.

I played with C#, and I liked the idea of strings. I'm having trouble passing char arrays into a function. I was wondering how one may do this.

I know it looks something like:

Code:
char(char* greeting)
But I'm not sure.

I'll think of more to ask, my head is a bit cloudy atm with some personal issues.
 
Old 09-30-2008, 01:16 PM   #2
virtualCoder
Member
 
Registered: Sep 2007
Distribution: Ubuntu
Posts: 33

Rep: Reputation: 15
Quote:
Code:
#include <iostream>

using namespace std;
int main()
{

	
	
	
	cout << "Enter your mpg: ";
	cin >> mpg;
}
I dont know about your Geany IDE but you need to declare a variable to store anything read from standard input.


Code:
#include <iostream>

using namespace std;

int main()
{
  string mpg;  // For a reading a string.
//  int num;  // For storing numbers without decimals

  cout << "Enter your mpg: ";
  cin >> mpg;
  return 0;
}
 
Old 09-30-2008, 01:23 PM   #3
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Quote:
As you can see in this: http://img296.imageshack.us/img296/7109/geanygz8.png There's a backspace pwn. I'm using:

Code:
#include <iostream>

using namespace std;
int main()
{

	
	
	
	cout << "Enter your mpg: ";
	cin >> mpg;
}
I was wondering how to fix that one.
what is a 'backspace pwn'? your code wont compile because 'mpg' isnt declared.

Quote:
I played with C#, and I liked the idea of strings. I'm having trouble passing char arrays into a function. I was wondering how one may do this.

I know it looks something like:

Code:
char(char* greeting)
But I'm not sure.

I'll think of more to ask, my head is a bit cloudy atm with some personal issues.
C++ was long before C#, and C++ came up with the string class first, so use that. to pass char arrays you can use what you have above. be careful not to go out of bounds with them when you are working with them in the function. if you are using a "char*", then use functions like "strlen" to get the length of the string so you know what the bounds are and dont go outside of them.

if you are passing a different kind of array, say int array, then there are two situations, for example:
Code:
void fooKnownArraySize(int ar[3])
{
  // here you are expecting an array with indices 0,1,2, so you know what the bounds are and it "should" work (ie the caller passed array of length 3
}

void fooNotKnowSize(int ar[])
{
  // this case would happen if you dont know the size of the array at runtime.  for example, reading a "string" from the user and converting each character
  // to its ASCII value and storing it into an int array (which is passed to this function). therefore the size of the array is determined at runtime
  // and this function doesnt know what size it is.
}

void fooNotKnowSize_fixed(int ar[], const int size)
{
  // this is similar to the previous function, however the size of the array IS known in the called function (ie where it looped over each character
  // to convert to ASCII, a count was kept). passing the int[] AND the known size of the array fixes this problem.
}
and finally, note that arrays are passed by reference, so changing them in a function will also change the array in the calling function. hope it helps

Last edited by nadroj; 09-30-2008 at 01:30 PM.
 
Old 09-30-2008, 01:28 PM   #4
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
I'm not quite sure what your first question is, so I'll leave that one for now, though I suspect what you mean is that entering a backspace at the prompt will produce undesired results -- if that is the case the answer is a bit more detailed than I feel up to this morning.

As for passing character arrays into a function, that is quite simple.
The address of the first element of an array and the address of a pointer to the array are identical.

The name of an array variable is synonymous with the address of first element of the array.


If your prototype is (i.e.):
Code:
int func(char *arg);
And your variable declaration is (again, i.e.):
Code:
char str[1024];
Then the call would be:
Code:
int ret = func(str);
This is all very basic C syntax, and you've said that you aren't exactly new to all this, so I'm a bit skeptical about whether I've really answered the question.

Arrays of character arrays are similar, and there are several methods for accessing them and passing them as arguments.
 
  


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
Dynamic array of function pointers - syntax needed seriesx4 Programming 6 01-05-2006 07:45 AM
pointers needed about installing/compiling from source PenguinE Linux - Newbie 5 01-02-2005 01:19 PM
Some GNOME 2.6 pointers needed Rajahuroman Linux - Software 2 05-23-2004 02:07 PM
pointers in C again h/w Programming 9 10-29-2003 11:46 PM
need help with pointers qanopus Programming 8 02-03-2003 05:09 PM

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

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