1

The prelude...

Where I work, people say I have the wrong terminology or invent my own. My boss says the right terminology is very important when working in a team.

The question

In C, what is the right term to use when we are referring to a pointer to any data type? For example I want to create macro functions of a send() function like this:

size_t send_stub (socket, void* , size_t);

#define send_str(sock, str) send_stub(sock, str, strlen(str))

then it will follow to create such a macro for data types with known size like pointers, byte, int16, int32, int64, arrays, structures, enums, but I would like to create a single macro function for all of them. What would it be called?

#define send_?(sock, ?) send_stub(sock, ?, sizeof(?))
Machavity
  • 30,841
  • 27
  • 92
  • 100
Edenia
  • 2,312
  • 1
  • 16
  • 33
  • Sorry but what do you mean by "macro function"? – Yunnosch Mar 05 '19 at 08:10
  • 2
    I would argue that you shouldn't use function-like macros at all, and always use `send_stub` directly at all times. That makes it more clear what you do. Macros generally tend to make code harder to read and understand. – Some programmer dude Mar 05 '19 at 08:11
  • Regarding the naming, why not call it `send_data`? – Sourav Ghosh Mar 05 '19 at 08:12
  • @SouravGhosh I wouldn't say this is very clear. Data can refer to so many things. – Edenia Mar 05 '19 at 08:12
  • @Yunnosch what an irony haha. Isn't a macro function the name of a macro as such #define x(y) – Edenia Mar 05 '19 at 08:12
  • 1
    All pointers only point to a single object/instance. So I think the correct name would be a "pointer". – Yunnosch Mar 05 '19 at 08:13
  • @Edenia Yet, that's what you want, one name to refer many things. Can you see the irony now? :) – Sourav Ghosh Mar 05 '19 at 08:13
  • @Yunnosch A pointer can also point to a beginning of string or a buffer though. – Edenia Mar 05 '19 at 08:14
  • I would call that a parameterized macro. Among other reasons it prevents people from misunderstanding you for talking about a function. – Yunnosch Mar 05 '19 at 08:14
  • @Yunnosch Good point. – Edenia Mar 05 '19 at 08:15
  • 1
    Ironic indeed. A macro is not a function. The correct term is *function-like macro*. It only looks the same, but the two concepts are very far apart. – StoryTeller - Unslander Monica Mar 05 '19 at 08:15
  • 3
    A macro which attempts to do the job of a function I would call a "bad idea". Now THAT is irony, on the edge to sarcasm. – Yunnosch Mar 05 '19 at 08:15
  • @Yunnosch It is a standard practice in GNU-like products. Also a macro implies something that saves time and does something automatically, so it is at least not reinventing the wheel or so to say – Edenia Mar 05 '19 at 08:16
  • @SouravGhosh haha yes. Well, data is a bit too broader than something that groups a pointer to a single instance of data type. – Edenia Mar 05 '19 at 08:18

1 Answers1

2

I think we can take our lead from the C standard itself here. I'll be quoting n1570, the C11 standard draft.

3. Terms, definitions, and symbols

3.15 object
1 region of data storage in the execution environment, the contents of which can represent values
2 NOTE When referenced, an object may be interpreted as having a particular type; see 6.3.2.1.

Seems to be right up your alley. If you assume the token provided as argument to the macro is an expression that evaluates to a pointer which points at a single object, then send_object seems appropriate.

#define send_object(sock, obj_ptr) send_stub(sock, (obj_ptr), sizeof *(obj_ptr))

If we also assume the pointer is to a complete object type, then sizeof *(obj_ptr) is the size of that object.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • I also like that object and string are words of the same lengths, aligns well when defined under the string macro haha. (If you thought that I am a fan of readability you are right). – Edenia Mar 05 '19 at 08:30