1

I've recently started learning C, so apologies if my question is very basic or unclear.

I'm passing 3 command line values to my program. ( TYPE , LENGTH , VAL). Using these values I want to create an array on the heap of type TYPE and length LENGTH and have all elements initialized to VAL. So for example

float 8 4 : will create a float array with 8 elements each of which will have a value of 4.

char 4 65 : will create a char array with 4 elements, each of which will have a value of 'A'.

Determining the length is straight forward. But I'm struggling to determine how to initialize the pointer to the array and the VAL.

The following is what I've attempted so far (unsuccessfully). I'm initializing a char pointer, however I know this is incorrect as I may need a float or int pointer instead. I'm also using a switch statement based on the first character of the TYPE, to me this seems very hacky.

int main (int argc, char *argv[]){

        int LENGTH;
        char *ptr;

        LENGTH = atoi ( argv[2] );


        switch( *argv[1] ){
                case 'f':
                        printf("Type is float\n");
                        ptr = (float *)malloc(LENGTH * sizeof(float));
                        break;
                case 'c':
                        printf("Type is char\n");
                        ptr = (char *)malloc(LENGTH * sizeof(char));
                        break;
                case 'i':
                        printf("Type is int\n");
                        ptr = (int *)malloc(LENGTH * sizeof(int));
                        break;
                default:
                        printf("Unrecognized type");
                        break;
        }

        while( i < arrayLength ){
                *ptr[i] = *argv[3];
                i++;
        }

        free ( ptr );

        return 0;
}

I can also see issues with the initialization element of the problem. The initial value is dependent on the TYPE so made need to be converted.

What I'm wondering is, without knowing the TYPE beforehand, how can I create the pointer or initialize the values? I'm probably looking at this issue from the wrong direction completely, so any advice would be greatly appreciated.

Thanks

4

3 回答 3

2

When want a variable in C that can hold different types, you need to use a union like:

typedef union {
    float f;
    int i;
    char c;
} arg_type;

Then a variable declared arg_type argument; will be large enough to hold the largest member of the union. They are accessed like structure members, for example:

argument.f = 0.0;
argument.c = '\n';
argument.i = 12;

However, you need to keep track of what type you did store in there because the compiler can't know which you used. Once the union is defined, you can do

char var_type = 'f';
arg_type *p = mallocLENGTH * sizeof(arg_type);
p[0].f = atof(argv[i])

This is kind of a pain to ensure that you've got it correct though. For example:

argument.f = 1234.56;
putchar(argument.c);

will compile but its behavior is undefined. It's often easier to leave things in argv character form and save the parsing until you want to work with a specific type.

于 2012-12-11T23:06:06.063 回答
2

Here is the code modified to do what you asked (see notes below):

#include <stdio.h>
#include <stdlib.h>
#include <string.h> // for memset()

int main (int argc, char *argv[]){

        int LENGTH;
        int i;

        void *ptr = NULL;

        if(argc < 4)
        {
            fprintf(stderr, "Usage: %s type len data\n", argv[0]);
            return 1;
        }

        LENGTH = atoi ( argv[2] );


        switch( *argv[1] ){
                case 'f':
                        printf("Type is float\n");
                        ptr = (float *)malloc(LENGTH * sizeof(float));
                        for(i = 0; i < LENGTH; i++)   // Cannot use memset in that case: memset(3) only works with integers.
                            ((float *)ptr)[i] = (float)atof(argv[3]);
                        for(i = 0; i < LENGTH; i++)
                            printf("[%d]: %lf\n", i, ((float *)ptr)[i]);
                        break;
                case 'c':
                        printf("Type is char\n");
                        ptr = (char *)malloc(LENGTH * sizeof(char));
                        memset(ptr, (char)atoi(argv[3]), LENGTH);
                        for(i = 0; i < LENGTH; i++)
                            printf("[%d]: %c\n", i, ((char *)ptr)[i]);
                        break;
                case 'i':
                        printf("Type is int\n");
                        ptr = (int *)malloc(LENGTH * sizeof(int));
                        memset(ptr, (int)atoi(argv[3]), LENGTH);
                        for(i = 0; i < LENGTH; i++)
                            printf("[%d]: %d\n", i, ((int *)ptr)[i]);
                        break;
                default:
                        printf("Unrecognized type\n");
                        break;
        }

        if(ptr == NULL)
            return 1;

        free ( ptr );

        return 0;
}

Still, as others pointed out, doing so is pointless. The reason why types are used is to know two things:

  1. The size of the data
  2. How to interepret it

What you are trying to achieve here is a clumsy attempt to do the job of the OS.

To be clear: memory is a contiguous storage and the OS marks it as "allocated" when it is (by functions like malloc(3)). When it does so, the program has to know the type of a data to know what to read (a pointer is merely an address), and how to interpret it (ints are not stored the same way that floats are).

If you are genuinely interested in learning such things (how data is stored, how computer works more generally, and at a lowest level than in C#), I'd recommend you to read:

  1. MIPS assembly tutorial (first relevant answer from google, any MIPS asm course would be okay)

  2. x86 Assembly, 6th ed. if you got the money for it.

Also, if you want to test MIPS assembly, mars is a MIPS assembly simulator that works ok (and is free).

I'm glad to see that people are still trying to learn C.

于 2012-12-11T23:19:55.587 回答
1

It's difficult to write such generic things in C. C++ is more appropriate. But if you need it in C, then what I would do is declare 3 different pointers, float*, int*, char*. I would allocate and initialize the one corresponding to the user input. Bear in mind, though, that you will need to carry with you some variable to flag what the chosen type is throughout the entire program, and use if statements whenever you need to use the array.

于 2012-12-11T22:58:25.057 回答