how to catch a parameter that is of the wrong type


 
Thread Tools Search this Thread
Top Forums Programming how to catch a parameter that is of the wrong type
# 1  
Old 03-23-2006
how to catch a parameter that is of the wrong type

Hi there,

Here is the prototype for the myConcat function which takes a variable number of arguments. I am to create a file that contains a definition of the myConcat function.

char* myConcat(int n, ... )

When the function is called, the first argument should be an integer > 0.
The other arguments should all be of type char* and are expected to point to '\0'-terminated sequences of chars. The first argument indicates how many "string" arguments are passed. myConcat should return the concatenation of these strings.

Note that the file must not contain a main() function. The only names visible outside the file (when it is compiled together with other .c files) should be "myConcat".


How do I make sure that the proper arguments have been passed to myConcate? For example myConcate(3, "Hello", "There", "!") would be correct, but myConcate(3, "Hello", 5.0, "There") should generate some kind of error because 5.0 is not a pointer to char.

Also, is there anything I have to do to make sure that the only names visible outside the file should be "myConcat"?


Thanks to anyone whose able to give me some kind of hint!
Magda
# 2  
Old 03-25-2006
C doesn't provide a way to check the datatype of an argument, which is why the compiler does it. variable argument lists are an exception to this.

It is the responsibility of the calling function to make sure the arguments are correct - in this case.

For example, printf() is in the same boat that your function is. It will print garbage or even segfault in this case:

Code:
double z=13.1;
printf("%s\n",z);

Note that printf cannot check to see if "z" is a character array. You code has the same issue.
[/code]
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

What is wrong with 'find . -maxdepth 1 -ctime -7 -type f'?

Have you tried running the command below? On the same RHEl 6.8 or 6.6. It will give you different output. find . -maxdepth 1 -ctime -7 -type f rpm -qa|grep find findutils-4.4.2-9.el6.x86_64 # cat /etc/redhat-release Red Hat Enterprise Linux Server release 6.8 (Santiago) # (6 Replies)
Discussion started by: invinzin21
6 Replies

2. Shell Programming and Scripting

Why result is wrong here ? whether break statement is wrong ?

Hi ! all I am just trying to check range in my datafile pls tell me why its resulting wrong admin@IEEE:~/Desktop$ cat test.txt 0 28.4 5 28.4 10 28.4 15 28.5 20 28.5 25 28.6 30 28.6 35 28.7 40 28.7 45 28.7 50 28.8 55 28.8 60 28.8 65 28.1... (2 Replies)
Discussion started by: Akshay Hegde
2 Replies

3. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

4. Shell Programming and Scripting

Command that takes one parameter and then searches for the passed in parameter

Hi I am looking for a unix command or a small shell script which can takes one parameter and then searches for the passed in the parameter in any or all files under say /home/dev/ Can anyone please help me on this? (3 Replies)
Discussion started by: pankaj80
3 Replies

5. UNIX for Advanced & Expert Users

register parameter type

Hello, I like to register parameters of my programm to the shell (for tab usage). How to do that? (3 Replies)
Discussion started by: daWonderer
3 Replies

6. Programming

array type has incomplete element type

Dear colleagues, One of my friend have a problem with c code. While compiling a c program it displays a message like "array type has incomplete element type". Any body can provide a solution for it. Jaganadh.G (1 Reply)
Discussion started by: jaganadh
1 Replies

7. Shell Programming and Scripting

String type to date type

Can one string type variable changed into the date type variable. (1 Reply)
Discussion started by: rinku
1 Replies

8. Shell Programming and Scripting

how do I make dynamic parameter names? Or get the value of a parameter evaluated twi

Say I write something like the following: var1=1 var2=2 for int in 1 2 do echo "\$var$int" done I want the output to be: 1 2 Instead I get something like: $var1 $var2 (2 Replies)
Discussion started by: Awanka
2 Replies

9. Shell Programming and Scripting

wrong parameter passing!

Hi all I have a script which will take input as filename and passes it to a java program. It is as follows -------------------------------- FILENAME=$1 echo $FILENAME ${JAVA_HOME}/bin/java -cp DateProvider $FILENAME ------------------------------------------------- when I execute the same... (2 Replies)
Discussion started by: malle
2 Replies
Login or Register to Ask a Question