3

I tried let stringArr = newvec(12); and then attempted to initialize each spot in the array as such: let stringArr!i = newvec(5); but that returns an error telling me I cannot do that. Is there anyone here who can help me with this dinosaur language?

4
  • 1
    Wow, where is BCPL still in use? Commented Apr 6, 2015 at 21:48
  • @Barmar Twitter. Just kidding, a specific college course. Commented Apr 6, 2015 at 21:49
  • I don't see the ! character anywhere in the BCPL reference manual cm.bell-labs.com/cm/cs/who/dmr/bcpl.pdf Commented Apr 6, 2015 at 21:52
  • Ahh, it looks like that's in the 1979 version of BCPL, not the 1967 version. Commented Apr 6, 2015 at 21:54

2 Answers 2

3

The let keyword is used only for creating new local variables (also functions and possibly other things, but that's not really relevant for your question). So, the statement:

let stringArr = newvec(12)

is valid in creating the new variable stringArr or, more precisely:

  • a 12-cell anonymous vector somewhere; and
  • the stringArr variable holding the address of that vector.

However:

let stringArr!i = newvec(5)

is not valid, because stringArr!i isn't actually a new variable. It's simply the memory contents of cell number i in the already existing stringArr vector.

In other words, the statement:

let stringArr = newvec(12)

creates both the initial pointer cell and the second layer of pointers, the latter of which will not point to anywhere useful yet:

+-----------+    +-------------+
| stringArr | -> | stringArr!0 | -> ?
+-----------+    +-------------+
                 | stringArr!1 | -> ?
                 +-------------+
                 :      :      :
                 +-------------+
                 | stringArr!N | -> ?
                 +-------------+

And, since those second-layer pointers already exist, you shouldn't be using let to set them(a). The right way to do what you're trying to achieve is:

let stringArr = newvec(12)   // Create new vector AND variable,
                             //   set variable to point at vector.
stringArr!i := newvec(5)     // Create new vector, set existing
                                  cell to point at it.

(a) It's similar in C in that you wouldn't write:

int xyzzy[10];      // Make ten-element array.
int xyzzy[0] = 42;  // Set first element of array.

In that case, the second line isn't supposed to be defining a new variable, rather its intent is simply to set one of the existing elements to a given value. It should instead be:

int xyzzy[10];  // Make ten-element array.
xyzzy[0] = 42;  // Set first element of array.
Sign up to request clarification or add additional context in comments.

Comments

0

The solution is sound but both of my versions of BCPL (Martin Richard's and Robert Nordier's obcpl) complain about newvec() and also require := rather than = in the second line. I got it working with:

let stringArr = getvec(12)
stringArr!i := getvec(12)

John Boutland

2 Comments

Thanks for that edit, wogsland. I didn't know (and still don't) how to get the code in that format. John
John, I suspect you've probably figured it out in the 3.5 years since you posted this answer :-) But, just in case, you have to ensure there's a blank line before your code and that each line of code starts with four spaces. You can mark a text block and then use CTRL-K to code-indent it. Alternatively, there's a triple-backtick method as well but I never use that so can't advise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.