Referencing part of an array.

This is a discussion on Referencing part of an array. within the C++ Programming forums, part of the General Programming Boards category; Is it possible to reference (or point to) part of an array? Example: Code: char name[]={'b','j','a','r','n','e','

Thread: Referencing part of an array.

',}; char &jar=// 'j','a','r'...

  1. #1
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71

    Referencing part of an array.

    Is it possible to reference (or point to) part of an array? Example:

    Code:
    char name[]={'b','j','a','r','n','e','\0',};
    char &jar=// 'j','a','r'

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    char* jar = &name[1];

    now a cout << jar << endl; would print "jarne"

  3. #3
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    Quote Originally Posted by Perspective
    char* jar = &name[1];

    now a cout << jar << endl; would print "jarne"
    Is there a way to terminate it at 'r' (i.e. "jar")?

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    if you dont mind ruining the original string you can set the next char to '\0' (ie: name[4] = '\0';) otherwise you'll have to copy the part you want into a different memory location.

  5. #5
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    Quote Originally Posted by Perspective
    if you dont mind ruining the original string you can set the next char to '\0' (ie: name[4] = '\0' otherwise you'll have to copy the part you want into a different memory location.
    No, I didn't want to touch the original string. Thanks for your help.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You could write a "substring" class that does that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Do you know ahead of time how much of the string you will need or is that a runtime decision?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 09:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21