473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Pointer clarification.

I am a grad student trying to get my hands on c++. I am trying to code
simple programs using pointers and it would be great if some gurus
here point me to the correct method of referencing.

My code complies but crash's .. can someone point me the right way to
do it. Thanks in advance!

************************************************** ************************************************** ***************************
// pointers to structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
string title;
int year;
};

int main ()
{
string mystr;
int mov_num,i;
cout << " Enter the number of movies you want to enter " << endl;
cin >mov_num;

movies_t amovie[mov_num];
movies_t * pmovie[mov_num];
// strcpy(&amovie, pmovie);
// pmovie = &amovie;

for (i=0;i<mov_num;i++);
{
cout << "Enter title: " << endl;
getline (cin, pmovie[i]->title);
cout << "Enter year: " << endl;
getline (cin, mystr);
(stringstream) mystr >pmovie[i]->year;
}
for (i=0;i<mov_num;i++);
cout << "\nYou have entered:\n";
cout << pmovie[i]->title;
cout << " (" << pmovie[i]->year << ")\n";
system ("pause");
return 0;
}
Aug 26 '08 #1
2 1098
On 2008-08-26 21:42, Makwana wrote:
I am a grad student trying to get my hands on c++. I am trying to code
simple programs using pointers and it would be great if some gurus
here point me to the correct method of referencing.

My code complies but crash's .. can someone point me the right way to
do it. Thanks in advance!

************************************************** ************************************************** ***************************
// pointers to structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
string title;
int year;
};

int main ()
{
string mystr;
int mov_num,i;
This is C++, you declare the variables right before you use them for the
first time, in particular 'i' should be declared in the for-loop.
cout << " Enter the number of movies you want to enter " << endl;
cin >mov_num;

movies_t amovie[mov_num];
This is not allowed in C++, the size of the array have to be a constant.
If your compiler allows you to do that you need to tell it to disallow
extensions to the language. Instead of an array you should be using
std::vector in this situation, considering that you are using string-
streams I think you should know about them by now.
movies_t * pmovie[mov_num];
There is really no need for pointers in this program, they will only
make things more complicated than necessary, you can use the array (or
vector) directly.
// strcpy(&amovie, pmovie);
// pmovie = &amovie;

for (i=0;i<mov_num;i++);
for (int i = 0; i < mov_num; ++i)

Notice the lack of the semicolon at the end, it is very important. The
way you have written it only the semicolon is inside the loop, the code
below is not. The effect is that when the code below is executed 'i'
will be equal to mov_num, which is one past the end of the array, which
is probably why your program crashes.
{
cout << "Enter title: " << endl;
getline (cin, pmovie[i]->title);
cout << "Enter year: " << endl;
getline (cin, mystr);
(stringstream) mystr >pmovie[i]->year;
}
for (i=0;i<mov_num;i++);
Remove the semicolon, and add the brackets, if your text-editor supports
automatically indenting the code you should use this feature since it
will tell you which code belongs to which code-block.
cout << "\nYou have entered:\n";
cout << pmovie[i]->title;
cout << " (" << pmovie[i]->year << ")\n";
system ("pause");
return 0;
}
--
Erik Wikström
Aug 26 '08 #2
On 26 Aug., 21:42, Makwana <makw...@gmail.comwrote:
I am a grad student trying to get my hands on c++. I am trying to code
simple programs using pointers
There's your first problem. It is not so easy to write (nontrivial)
programs with pointers. Even experienced programmers might put in a
bug or two in this case.
and it would be great if some gurus
here point me to the correct method of referencing.

My code complies but crash's .. can someone point me the right way to
do it. Thanks in advance!

************************************************** ************************************************** ****************************
// pointers to structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
* string title;
* int year;

};

int main ()
{
* string mystr;
* int mov_num,i;
As Erik mentioned, declare variables when you use them - not before.
This habit might improve the performance of your program, but more
important it reduces the number of bugs.
* cout << " Enter the number of movies you want to enter " << endl;
* cin >mov_num;

* movies_t amovie[mov_num];
This is not valid C++. mov_num is not a compile-time constant - your
program should not compile.

Why amovie? This is a bad name for an array.
* movies_t * pmovie[mov_num];
This is a pointer to an array of movies - probably not what you want.
// *strcpy(&amovie, pmovie);
// *pmovie = &amovie;
Why these commented out statements - they make no sense.
>
* for (i=0;i<mov_num;i++);
* {
* cout << "Enter title: " << endl;
* getline (cin, pmovie[i]->title);
* cout << "Enter year: " << endl;
* getline (cin, mystr);
* (stringstream) mystr >pmovie[i]->year;
This is a C-cast. Never do that. What are you trying to do here? If
you want to read an integer simply do e.g. std::cin << pmovie[i]-
>year;
* }
* for (i=0;i<mov_num;i++);
This line is a complete for loop, that doesn't do anything.
* cout << "\nYou have entered:\n";
* cout << pmovie[i]->title;
* cout << " (" << pmovie[i]->year << ")\n";
* system ("pause");
* return 0;

}
Also your intendation is bad. Use copy-paste from your source-file.

/Peter
Aug 26 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Terence | last post by:
I need some clarification with pointer arithmetics on void *. Example 1: ======== char s; char *ptr = s; ptr += 1; // I assume ptr is increased by 1 byte, pointing to the 2nd element in the...
3
by: ma740988 | last post by:
Consider the 'C' source. void myDoorBellISR(starLinkDevice *slDevice, U32 doorBellVal) { doorBellDetected = doorBellVal; } void slRcv() { starLinkOpenStruct myOpenStruct;
11
by: Edd | last post by:
Hello all, I've made a data structure and an associated set of functions to enable me to store a dynamically-sized array of elements of whatever data type I like. Well that's the idea anyway......
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
2
by: Workgroups | last post by:
I need some clarification with the whole "no pointers in VB.NET" thing, because it seems like there are "quazi-pointers" happening but I want to make sure I'm interpreting all this correctly. ...
2
by: Chad | last post by:
The following question stems from the following thread on comp.lang.c: http://groups.google.com/group/comp.lang.c/browse_thread/thread/0ad03c96df57381a/5f20260b30952fe7?hl=en#5f20260b30952fe7 I...
8
by: Sam | last post by:
I have a situation occuring in my code and I just can't see to figure out why I have an structure called employee that will put all of the employee id's into a char array set to 10 struct...
13
by: arnuld | last post by:
i see the use of pointers, from K&R2 but what is the use of: 1. "pointer to pointer": char c; char** ppc; 2. pointer to function:
16
by: braton | last post by:
Hello Have a look at this little piece of code: signed char sca = {2,-1,4,3,5,3,-8,1,9,2,3,6,-1,0,12,2}; signed char sc = *(sca+1); short s = *((short*)(sca+sc)+4); My question is: does...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.