1

I found this sentence in a book.

A pointer is returned to an automatic variable in a previous function call (discussed in the section “Pointers to Local Data” on page 66).

Based on the context, I guess the author really mean a pointer to an automatic variable is returned in a previous function call. Is my understanding correct? If so, the current word order is a little bit misleading and it could let people think we are returning a pointer to a variable, which is pointless. Is the current word order correct?

  • In programming, pointers to objects (including variables) can be returned. In fact, it's a common technique. So, this is not an obvious error, or even necessarily an error at all. The most questionable thing about the sentence is calling something an automatic variable. I don't know what that means—although it's possible it's a variable type I've never heard of. But unless it is, that language doesn't make sense. – Jason Bassford Aug 4 at 6:19
  • @JasonBassford Thanks for the comment. Automatic variable here just means local variable and it's a C Language term. In the book, the author states that you should not return a pointer to a local variable out of a function. Because when the function completes, the local variable disappears and its memory can be reused. – Just a learner Aug 4 at 6:42
  • Could it possibly mean "(a pointer to [an automatic variable in a previous function call]) is returned"? I agree the word order is odd, and it should probably be broken into two sentences. – Jack O'Flaherty Aug 4 at 7:15
  • @JackOFlaherty It's hard to be sure without context, but that is my interpretation too (and I don't actually find the word order strange). Since the section title is "Pointers to Local Data," the book might be discussing the common mistake, in some languages like C/C++, of returning a pointer to variables that have gone or will go out of scope (this would be the case for variables of automatic storage duration in C++, also known as local variables). – TypeIA Aug 4 at 8:07
  • @Justalearner Ah, okay. I had always heard of local and global variables when I used to work with C. It seems automatic is new terminology. – Jason Bassford Aug 4 at 14:38
3

Your understanding may be correct. I believe your rephrasing, "a pointer to an automatic variable is returned", is very likely what the original writer meant. That, however, leaves the final part of the sentence, "in a previous function call," to be placed with 'a pointer' or with 'an automatic variable'.

Based on its location in the original reference, I would assume the whole sentence would be rewritten, "A pointer to an automatic variable in a previous function call is returned."

It is unlikely that the original writer meant, "A pointer in a previous function call is returned to an automatic variable."

Since the original sentence is a bit confusing, as you note, it's difficult to know exactly what the writer meant.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.