How to get the size of the datatype passed as the command line argumet?


 
Thread Tools Search this Thread
Top Forums Programming How to get the size of the datatype passed as the command line argumet?
# 1  
Old 03-25-2011
How to get the size of the datatype passed as the command line argumet?

Code:
#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("%d\n", sizeof(argv[1]));
    return 0;
}

when I run the executable a.out after compiling the above program as:
a.out short (or) a.out "long double", I expected to get the output as 2 and 12, but I am always getting the size of argv[] char array as 4 bytes

How to solve this?
# 2  
Old 03-25-2011
I think the only way to do this is to scan the input and then have according printfs.

Code:
if(!strcmp(argv[1], "int")) 
    printf("%d\n", sizeof(int));
else if(!strcmp(argv[1], "double")
    printf("%d\n", sizeof(double));
else if(...)

The reason is that sizeof returns the size of the VARIABLETYPE that you give it. It does not care about its content. In your case it would be sizeof(char *). It does not match any strings or so.
This User Gave Thanks to disaster For This Post:
# 3  
Old 03-25-2011
sizeof() doesn't work that way. It happens at compile-time -- the compiler decides what size it is once, and doesn't ever change later. It doesn't understand strings anyway -- you're getting the size of a pointer.

You'll just have to do it by brute force.

Code:
const struct sizes_t
{
        const char *str;
        int size;
} sizes[]={
 { "char", sizeof(char) }, { "short", sizeof(short) }, { "int", sizeof(int) },
 { "long", sizeof(long) }, { NULL, 0 } };

int main(int argc, char *argv[])
{
        int n;
        for(n=0; sizes[n].str != NULL; n++)
        {
                if(strcmp(argv[1], sizes[n].str) == 0)
                {
                        printf("sizeof %s = %d\n", sizes[n].str, sizes[n].size);
                        break;
                }
        }
}

You could do a little checking like, check if the string begins with 'unsigned' and ignore it if so since sizeof(x) == sizeof(unsigned x).
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. How to Post in the The UNIX and Linux Forums

Read a json file passed as cmd line argument

usage: myscript.sh config.json config.json: { "HOST":"abc", "DB_NM":"xyz", "USR_NM":"asd", "PWD":"xxx", ......... ......... ......... ........ } myscript.sh: (2 Replies)
Discussion started by: RGRT
2 Replies

2. Shell Programming and Scripting

Variable not passed to the sed command

Hello, I am writing a script which is not giving the desired result. When I check the content of the 'InputFile_009_0.sh', it shows following with missing Index in this command sed -i "s/L1ITMBLT.root/L1ITMBLT_"".root/g" run_DttfFromCombinedPrimitives_cfg.py of . Any help? ... (13 Replies)
Discussion started by: emily
13 Replies

3. UNIX for Dummies Questions & Answers

Looking for command line to find dirs based on size and date

Hi, My first time on this site, please excuse me if I've come to the wrong forum. I'm fairly new to Unix/Linux and hoping you can help me out. I'm looking for a command line that will return a list of directories that are larger than 50M and older than 2 days. I thought it may be... (6 Replies)
Discussion started by: Wisconsingal
6 Replies

4. Shell Programming and Scripting

Datatype and length validation

I have sourcefile and structure of source file,i want to check whether datatype and length mention in emp.txt is same as source file. Example: in emp.txt first row contains sno number so in source file also first column should contain only number if data is other than number then that... (1 Reply)
Discussion started by: katakamvivek
1 Replies

5. Shell Programming and Scripting

Datatype file validation

I have a sourcefile which contains data as below.I want to check whether datatype,structure and date format looks good as mentioned. Data is delemited by cydila . Source file-Emp.txt sno name phoneno dept joineddate 1 vivek 0861 CSE 2013-05-29 00:00:00 2 dinesh 123456 ECE ... (2 Replies)
Discussion started by: katakamvivek
2 Replies

6. Shell Programming and Scripting

Help with separating datatype, column name

Hi All, I am new to unix but have a requirement wherein I need to separate datatype,length, and column name from input file which is of below format -- record integer(10) empid; string(25) name; date("YYYY-MM-DD") dob; decimal(10) salary; end now after getting datatype,its length and... (4 Replies)
Discussion started by: phoenix09
4 Replies

7. Shell Programming and Scripting

shell script for ftp files passed in command line argument

i want to write a shell script function that will ftp the files passed in the command line . i have written a shell script for ftp but how will it do for all files passed in command line argument , i am passing 4 files as argument ./ftp.sh file1 file2 file3 file4 code written by me... (5 Replies)
Discussion started by: rateeshkumar
5 Replies

8. UNIX for Advanced & Expert Users

To get PID of a command passed to a secondary system

Hi Reqmt: I am working on Sys 1 and passes a command to Sys2 using a utility which has ssh coded in it. Is there any way to get the process id of the command in Sys 2 from my Sys1 ? Note: The utility is not editable, so you cannot get it through it and I am not logged in in Sys2. Thanks... (2 Replies)
Discussion started by: anu_math9
2 Replies

9. Solaris

command to find out total size of a specific file size (spread over the server)

hi all, in my server there are some specific application files which are spread through out the server... these are spread in folders..sub-folders..chid folders... please help me, how can i find the total size of these specific files in the server... (3 Replies)
Discussion started by: abhinov
3 Replies

10. UNIX for Advanced & Expert Users

How to change size of command line in unix

Hi, I'm trying to execute my program from $prompt by passing many parameters which is more than 300 charecters in line but unix not accepting those many charecters, could some one help me how to increase the size? thanks (7 Replies)
Discussion started by: krishna
7 Replies
Login or Register to Ask a Question