Thread: Referencing part of an array.

  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,895
    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, 07:48 PM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM