1

I need to make an adfgx machine (Code language from WWII) for a project in school. But I am running into some problems.

There is a struct along with some functions defined in adfgx.h which looks like this:

typedef struct {
   char* alphabet;         
   char* symbols;          
   char* dictionary;       
   char* transposition;    
} adfgx;

In adfgx.c we include the header and I have to write a function that allocates memory for this struct with a predefined signature:

/* Creates a new ADFGX machine */
adfgx* create_adfgx(const char* alphabet, const char* symbols, const char* dictionary, const char* transposition);

So what I am supposed to do here is allocate memory for the struct in that function. I don't get how I am supposed to do this, because I don't now the size of alphabet, symbols, dictionary and transposition, so how can I now hom much memory I need to allocate?

0

2 Answers 2

1

I don't now the size of alphabet, symbols, dictionary and transposition

Since the size is not passed in, the API must assume that const char* parameters represent C strings. This makes it possible to compute the desired size by calling strlen, and adding one to the result for the null terminator.

To avoid doing the same thing multiple times, I suggest defining your own function for string duplication:

static char *duplicate(const char *s) {
    if (s == NULL) return NULL;
    size_t len = strlen(s)+1;
    ... // Call malloc, check results, make a copy
    return res;
}

Now you can use this function to populate the struct:

adfgx *res = malloc(sizeof(adfgx));
if (res == NULL) ...
res->alphabet = duplicate(alphabet);
...
return res;
Sign up to request clarification or add additional context in comments.

3 Comments

Any particular reason to suggest writing your own variant of strdup() without mentioning that it is what you're suggesting?
@JonathanLeffler This is almost certainly a learning exercise, and writing their own version of strdup is probably its centerpiece. If professor wanted them to use strdup, OP would probably never asked this question :-)
One possible interpretation, yes. It isn't the one that occurred to me, but it is far from impossible. Another possible interpretation is that the function groups its arguments into a structure for convenience; the strings pointed at must last as long as code will be using the structure. Then all that's needed is to copy the pointers into the allocated structure. In that case, strdup() is not needed at all. Still, on average, it is more likely that the strings must be duplicated, but I'm not sure whether strdup() is verboten. I would have mentioned it -- these comments now suffice.
1

The size of adfgx does not depend on "the size of alphabet, symbols, dictionary and transposition" - it's the size of 4 pointers.

As to assigning values to newAdfgx->alphabet and other members, you can use strdup. Just be sure to free() the strduped strings when you free the instance of adfgx.

Comments

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.