LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-14-2014, 02:33 PM   #1
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Rep: Reputation: 0
Segmentation fault when passing user input to a pointer to an array


Hi Everyone, I am working with pointers. I am tryin to input a value into a double array, and then output this value not with the double array variable itself but instead, I would like to use the pointer to the double array variable to output the user's input.

Here is my code.

#include <iostream>
using namespace std; // cin and cout

int main()
{
double weight[50]; // variable array weight of type double
double * p_pw; // pointer to weight pw

p_pw = &weight[50]; // pw is assigned the address of the weight array

cout << "input a value for your weight please: " << endl;
cin >> weight[50]; // store input in weight array

* p_pw = weight[50];

cout << " The weight that you inputed is \n" ; //
cout << * p_pw << endl; // should return the input from user


return 0;


} // end main

The program compiles fine, the segmentation fault error is listed right after I submit input. Here is a image of the error message I recieve.

jonathan@ubuntu:~/c++practice/chapter4$ ./a.out
input a value for your weight please:
23
Segmentation fault
 
Old 02-14-2014, 03:03 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by UnixCube View Post
double weight[50]; // variable array weight of type double
double * p_pw; // pointer to weight pw

p_pw = &weight[50]; // pw is assigned the address of the weight array
If your comment "pw is assigned the address of the weight array" were correct, you should have:
p_pw = weight;
or equivalent
p_pw = &weight[0];

Your version sets p_pw to point to the address of the 51'st element of a 50 element array (in other words to the next object on the stack after that array).

Quote:
Originally Posted by UnixCube View Post
cout << "input a value for your weight please: " << endl;
cin >> weight[50]; // store input in weight array

* p_pw = weight[50];
But the rest of your code makes so little sense, I can't guess what you were trying to do.

Whatever it was should not be done that way.

An array declared with size 50 has elements 0 through 49. It does not have an element 50.

Edit: On review, I think I do have a guess what you may have been trying to do:
Code:
#include <iostream>
using namespace std; // cin and cout

int main()
{
double weight[50]; // variable array weight of type double
double * p_pw; // pointer to weight pw

p_pw = &weight[49]; // pw is assigned the address of the last element of weight array

cout << "input a value for your weight please: " << endl;
cin >> weight[49]; // store input in last element of weight array

cout << " The weight that you inputed is \n" ; //
cout << * p_pw << endl; // should return the input from user


return 0;


} // end main
Notice I did not include your instruction
* p_pw = weight[50];
Even corrected to 49 instead of 50, that would confuse the example. The earlier code makes *p_pw the same object as weight[50]. Changing one of them changes the other. Copying one to the other is redundant.

The important change I made was to use the last element of the array instead of one past the last element.
Since your comments obscured your intention (if I'm now guessing it correctly), I also changed those.

Last edited by johnsfine; 02-14-2014 at 03:17 PM.
 
1 members found this post helpful.
  


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
[SOLVED] C: Segmentation fault when freeing allocated 2D array aihaike Programming 2 06-01-2011 11:12 PM
C segmentation fault, pointer lwhat Programming 2 11-22-2009 07:23 AM
pointer to pointer segmentation fault Guilherme Programming 7 06-28-2009 09:47 AM
Segmentation fault with char* array simopal6 Programming 8 06-16-2006 01:05 PM
checking pointer is non-null causes segmentation fault in c++ markhod Programming 10 01-11-2005 11:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:07 PM.

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