Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:33:54+00:00 2026-06-15T23:33:54+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:33:56+00:00Added an answer on June 15, 2026 at 11:33 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a view passing on information from a database: def serve_article(request, id): served_article
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.