How to find out if an additional argument to a C function has been passed

Edit 3: for the code itself, collectively check the first response or the end of this post.

As indicated in the header, I am trying to find a way to find out if an extra argument was passed to the function or not. What I'm trying to do is similar to the way almost all dynamic languages ​​process their subscript function. Below mine, but it does not work, since I do not know how to say if / when a thing is used.

char *substring(char *string,unsigned int start, ...){
    va_list args;
    int unsigned i=0;
    long end=-1;
    long long length=strlen(string);
    va_start(args,start);
    end=va_arg(args,int);
    va_end(args);
    if(end==-1){
        end=length;
    }
    char *to_string=malloc(end);
    strncpy(to_string,string+start,end);
    return to_string;
}

Basically, I want me to not be able to include the length of the string that I want to return, and just get to the end of the string. But I can’t find a way to do this. Since there is also no way to find out the number of arguments passed to C that took away my first thought about this.

Edit: A new way to do this here in the current code.

#define substring(...) P99_CALL_DEFARG(substring, 3, __VA_ARGS__)
#define substring_defarg_2 (0)
char *substring(char *string,unsigned int start, int end){
    int unsigned i=0;
    int num=0;
    long long length=strlen(string);
    if(end==0){
        end=length;
    }
    char *to_string=malloc(length);
    strncpy(to_string,string+start,end);
    return to_string;
}

test.c, , .

#include "functions.c"
int main(void){
    printf("str:%s",substring("hello world",3,2));
    printf("\nstr2:%s\n",substring("hello world",3));
return 0;
}

functions.c include.h, , - . clang ( clang, , .

In file included from ./p99/p99.h:1307:
./p99/p99_generic.h:68:16: warning: '__error__' attribute ignored
__attribute__((__error__("Invalid choice in type generic expression")))
               ^
test.c:4:26: error: called object type 'int' is not a function or function
      pointer
    printf("\nstr2:%s\n",substring("hello world",3));
                         ^~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from test.c:1:
In file included from ./functions.c:34:
In file included from ./functions.h:50:
./string.c:77:24: note: instantiated from:
#define substring(...) P99_CALL_DEFARG(substring, 3, __VA_ARGS__)

GCC ,

2: , -1 , . , , .

gcc -std = c99 -c test.c -o test -lm -Wall

Clang - ( , , .

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include "p99/p99.h"
#define substring(...) P99_CALL_DEFARG(substring, 3, __VA_ARGS__)
#define substring_defarg_2() (-1)
char *substring(char *string, size_t start, size_t len) {
  size_t length = strlen(string);
  if(len == SIZE_MAX){
    len = length - start;
  }
  char *to_string = malloc(len + 1);
  memcpy(to_string, string+start, len);
  to_string[len] = '\0';
  return to_string;
}

p99. . , . . , , . , , .

, , , , , , , , , .

+3
4

C , , va_arg . va_arg, , . , P99 .

-

char *substring(char *string, unsigned int start, int end);

end, , -1. P99

#include "p99.h"

#define substring(...) P99_CALL_DEFARG(substring, 3, __VA_ARGS__)
#define substring_defarg_2() (-1)

, , "" (, , C ) , (3 ). , , _defarg_N, N 0.

, , , , , va_arg . ( "" ). -

substring("Hello", 2); 
substring("Holla", 2, 2);

.

( , C99 .)


: , end, . :

char *substring2(char *string, unsigned int start);
char *substring3(char *string, unsigned int start, unsigned int end);

#define substring(...)                \
 P99_IF_LT(P99_NARG(__VA_ARGS__, 3))  \
 (substring2(__VA_ARGS__))            \
 (substring3(__VA_ARGS__))

, , , .


Edit2: substring:

  • , ..
  • , , .
  • strncpy , , '\0'. , memcpy.

char *substring(char *string, size_t start, size_t len) {
  size_t length = strlen(string);
  if(len == SIZE_MAX){
    len = length - start;
  }
  char *to_string = malloc(len + 1);
  memcpy(to_string, string+start, len);
  to_string[len] = '\0';
  return to_string;
}
+4

C , . , ; substr(char *, size_t start, size_t end) substr_f(char *, size_t start) , end, , (, , , , start 0).

varargs (, NULL) , argc ().

C , , .

: , C, size_t. , , , , , .

, .

+6

, va_arg :

, va_arg , , ( , ). , - , .

" " "" , right_substr. , , , .

, left_substr, substring right_substr , start length , . , "" , , , .

+2

In the C standard, when prototypes of variable arguments ( ...) are used, there is no way to directly determine how many arguments are passed.

Behind the scenes, functions, printf()etc., take the number of arguments based on a format string.

Other functions that accept, say, a variable number of pointers, expect the list to be completed using NULL.

Consider using one of these methods.

+1
source

All Articles