Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 3rd, 2006, 10:06 AM
Authorized User
 
Join Date: Apr 2006
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default How to convert subscripts to pointers

Hello,

How do I convert the following to use pointers instead of subscripts?

short i;
char words [2000];
for (r=0; r < NumRows; r++)
      cout << words[r];

In the assignment we were given we were told to dynamically allocate an array, read an input file of strings, and store each string in the array preceeded by a one-byte field containing the length of that string. Further the assignment says to not leave unused elements after each string.

The only way I can figure out how to do this is to create another temporary array and do a memcpy after properly positioning the pointer and then do the cout from the temporary array.

Sam Stamport
__________________
Sam Stamport
Reply With Quote
  #2 (permalink)  
Old May 4th, 2006, 08:20 AM
Authorized User
 
Join Date: Apr 2006
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Let me clarify this a bit.

The code I put in my original post works when the array is defined at compile-time, according to my instructor. However, the assignment we were given was to dynamically allocate the array. As I understand it pointers HAVE to be used anytime an array is dynamically allocated.

Since the assignment also says that the dynamically allocated array is not to be treated as a 2D array -- we are to not leave empty elements at the end of each "row" -- to print the array would require that another temporary array be allocated, the input line extraced from the original array using memcpy, then the temporary array printed in a loop.

The original array is defined as "char", but we are to use strlen to place the length of each string in the byte immediately preceeding each string, the contents of the string (including the NULL character at the end), then at the very next element the lenght of the next record, the contents of the record, and so on.

Sam Stamport
Reply With Quote
  #3 (permalink)  
Old May 5th, 2006, 09:06 AM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

To dynamically allocate an array would look something along the lines of this:

char* myString;

// c++ new operator
myStrings = new char[10]; // a string containing 10 elements

// c style calloc call

char* myString;

myString = (char*) calloc(40, sizeof(char)); // returns a pointer to an array 40 elements
                                                                     // wide each element size of a char
you Can re-allocate a block with realloc

myString = (char*)realloc( myString, (_msize(myString) + (10*sizeof(char)))); // new array with 10 more elements all size of a char
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ Pointers reality_42000 C++ Programming 1 March 4th, 2007 01:26 AM
A little clueless, need some pointers bmg181 ASP.NET 2.0 Basics 3 December 23rd, 2005 01:21 PM
pointers Stuby085 Visual C++ 1 August 30th, 2003 11:58 PM
Pointers and Arrays C++ odie All Other Wrox Books 1 July 10th, 2003 01:45 PM
Pointers jake VB How-To 12 June 21st, 2003 11:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.