8 votes

Fortnightly Programming Q&A Thread

General Programming Q&A thread! Ask any questions about programming, answer the questions of other users, or post suggestions for future threads.

Don't forget to format your code using the triple backticks or tildes:

Here is my schema:

```sql
CREATE TABLE article_to_warehouse (
  article_id   INTEGER
, warehouse_id INTEGER
)
;
```

How do I add a `UNIQUE` constraint?

1 comment

  1. unknown user
    Link
    The answer is probably “No”, but still. Is there a way to get a “private structure” in plain C? The closest I've got is this: metres.h: typedef struct metres metres_t; int...

    The answer is probably “No”, but still. Is there a way to get a “private structure” in plain C? The closest I've got is this:

    • metres.h:
      typedef struct metres metres_t;
      
      int       metres_get_value(metres_t *x);
      metres_t *metres_add(metres_t *x, metres_t *y);
      metres_t *metres_new(int value);
      void      metres_delete(metres_t *x);
      
    • metres.c:
      #include <stdlib.h>
      // NULL
      // free
      // malloc
      
      #include "metres.h"
      
      struct metres {
      	int value;
      };
      
      int metres_get_value(metres_t *x)
      {
      	if (x == NULL) {
      		return 0;
      	}
      
      	return x->value;
      }
      
      metres_t *metres_add(metres_t *x, metres_t *y)
      {
      	if (x == NULL || y == NULL) {
      		return NULL;
      	}
      
      	metres_t *z = malloc(sizeof(metres_t));
      	if (z == NULL) {
      		return NULL;
      	}
      
      	z->value = x->value + y->value;
      	return z;
      }
      
      metres_t *metres_new(int value)
      {
      	metres_t *x = malloc(sizeof(metres_t));
      	if (x == NULL) {
      		return NULL;
      	}
      
      	x->value = value;
      	return x;
      }
      
      void metres_delete(metres_t *x)
      {
      	free(x);
      }
      
    • main.c :
      #include <stdio.h>
      // printf
      
      #include "metres.h"
      // metres_add
      // metres_delete
      // metres_get_value
      // metres_new
      // metres_t
      
      int main(void)
      {
      	metres_t *x = metres_new(5);
      	metres_t *y = metres_new(4);
      	metres_t *z = metres_add(x, y);
      
      	printf("the result is: %dm\n", metres_get_value(z));
      
      	metres_delete(x);
      	metres_delete(y);
      	metres_delete(z);
      
      	return 0;
      }
      

    This works, but forces API users to use heap allocation at all times, because otherwise they get “incomplete type” errors when trying to use automatic allocation for metres_t.

    (Inspired by this question on Russian StackOverflow.)

    2 votes