I am writing some C in which the program is going to convert the first command line argument into a int and check to see if it is an int. If it isnt a integer value it will then attempt to check to see whether the string begins with a '.' character or not. For some reason I am getting an undefined reference. How is this an undefined reference when it seems to be defined?
Here is the following code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
int startswith(char,char);
int main(int argc, char * argv[]) {
int forst;
srand(time(NULL));
int speed_delay = rand() % 20;
printf("The speed delay is:%i\n", speed_delay);
int first = atoi(argv[1]);
printf("First:%i\n", first);
if (first == 0) {
//this means text was inserted instead of a number
if (startswith(first, '.')) {
printf("string starts with a period !");
}
} else {
}
int startswith(const char * one,
const char * two) {
if (strncmp(one, two, strlen(two)) == 0) {
return 1;
}
return 0;
}
}
Your declaration and definition of startswith are incompatible.
The declaration has two parameters of type char, but the function actually has two parameters of type const char *.
You also defined startswith inside of main. Functions cannot be nested.
Because no function exists that matches the declaration, you have an undefined reference.
Fix your declaration to match the definition.
int startswith( const char *, const char *);
You're also not calling this function correctly. You pass in an int and a char. It should be called like this:
if(startswith(argv[1],"."))
Try replacing int startswith(char,char); with below
static inline const char *startwith(const char *s, const char *prefix)
{
size_t sz = prefix ? strlen(prefix) : 0;
if(s && sz && strncmp(s, prefix, sz) == 0){
return s+sz;
}
return NULL;
}
Related
I am trying to code the printf function. The problem is that my code is getting very messy and I need some help to try to make it organized and working (hopefully). I have been told that I should use "array of function pointers" so I tried below (ft_print_it) as you can see but I do not know how to how to structure my code so that I can use a big array of function pointer to put every function like int_decimal_octal and friends. Can you help me on that? Where can I call them from?
Also, I realized the little function below (cast_in_short) is giving me the same result as printf if I write the output with my ft_putnbr. My second question is thus: Can I make my printf work with little functions like this? Thank you so much.
int cast_in_short(int truc)
{
truc = (short)truc;
return (truc);
}
/*
here in the main I noticed that I get the same behaviour
between my putnbr and printf thanks to my little function
cast_in_short. This is the kind of function I want to use
and put into an array of pointer of functions in order
to make my printf work
*/
int main()
{
int n = 32769;
n = cast_in_short(n);
ft_putnbr(n);
printf("\n");
return (0);
}
/* function to launch ft_print_it */
int ft_print_str_spec(va_list ap, char *flag)
{
if (ft_strlen(flag) == 1)
ft_putstr(va_arg(ap, char *));
else
{
ft_nbzero(ap, flag, 0);
ft_putstr(va_arg(ap, char *));
}
return (1);
}
int ft_print_oct(va_list ap, char *flag)
{
if (ft_strlen(flag) == 1)
ft_putnbr(decimal_octal((va_arg(ap, int))));
else
{
ft_nbzero(ap, flag, 1);
ft_putnbr(decimal_octal((va_arg(ap, int))));
}
return (1);
}
#include "libft.h"
#include <stdarg.h>
#include <stdio.h>
char *ft_strjoin2(char const *s1, char const c);
#include "libft.h"
#include <stdarg.h>
#include <stdio.h>
int decimal_octal(int n) /* Function to convert decimal to octal */
{
int rem;
int i;
int octal;
i = 1;
octal = 0;
while (n != 0)
{
rem = n % 8;
n /= 8;
octal += rem * i;
i *= 10;
}
return (octal);
}
I think the best way to organize your code to avoid the function like your "flag_code" is to use an array of structure. With structure that contain a char (corresponding to the flag) and a function pointer.
For example :
typedef struct fptr
{
char op;
int (*ptr)(va_list);
} fptr;
And instatiate it like that (with { 'flag', name of the corresponding function} ) :
fptr fptrs[]=
{
{ 's', ft_print_nb_spec },
{ 'S', ft_print_nb_up },
{ 0, NULL }
};
Then when you know have char after the % (the flag) you can do something like this :
int i = -1;
while (fptrs[++i].op != flag && fptrs[i].op != 0);
if (fptrs[i].op != 0)
{
fptrs[i].ptr();
}
For exemple if flag = 'S' the while loop will stop when i = 1 and when you call fptrs[1].ptr() you will call the corresponding function in the structure.
I think instead of making your code messy by using function pointers, because in the end you cannot specialize printf function without providing the format, in C there is no function overloading or template functions. My suggestion is to special printf function by type.
// print seperator
void p (int end)
{ printf(end?"\n":" "); }
// print decimal
void pi (long long n)
{ printf("%lld",n); }
// print unsigned
void pu (unsigned long long n)
{ printf("%llu",n); }
// print floating point
void pf (double n)
{ printf("%g",n); }
// print char
void pc (char n)
{ printf("%c",n); }
// print string
void ps (char* n)
{ printf("%s",n); }
Test try here
pi(999),p(0),pf(3.16),p(0),ps("test"),p(1);
Output
999 3.16 test
Another option
In theory you can define polymorphic print function in a struct, in case you can do something like this. I haven't tested this yet.
struct Node
{
enum NodeType {Long,Double,Char,String} type;
union {long l,double d,char c,char* s};
};
void p(Node* n)
{
switch (n->type)
{
case Node::NodeType::Long: printf("%ld", n->l);
case Node::NodeType::Double: printf("%g",n->d);
case Node::NodeType::Char: printf("%c",n->c);
case Node::NodeType::String: printf("%s",n->s);
}
}
char names [MAX_CLASS_SIZE][MAX_NAME_SIZE+1]={"Julias Hoffman","Dianne Conner","Mitchell Cooper","Johnnie Greene","Johanna Mason","Kevin Adkins","Brandi Spencer","Marian Tyler","Chester Cross","Martin Lawrence","Jane Smith","Sara Jones"};
char specificName[]="";
int search(char names[][MAX_NAME_SIZE+1],char specificName[])
for(i=0;i<MAX_CLASS_SIZE;i++){
if (strcmp(names[i],specificName)==0)
{
printf("Found %s",names[i]);
return i;
}
}
This function receives an array of strings, called name and an array of characters that represents a specific name. This function searches through the array name for the specific name and returns the index of the specific name if it is found, -1 if it is not found.
In main() I ask the user for a name and store it in the specificName char array using scanf("%s",specificName);. Then in the search() function I am attempting to search the array of names for the user given name and return a certain value based on if the name is found. I have included the necessary libraries. I am trying to do this using strcmp, but search() doesn't find the matching name.
You could try the following approach:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int search(char **names,char *specificName, size_t length){
size_t i,found=0;
for(i=0;i<length;i++){
if (strcmp(*(names + i),specificName)==0){
found = 1;
}
}
return found == 1 ? 0 : 1;
}
int main(void) {
size_t length;
char specificName[100];
char *names[12]={"Julias Hoffman","Dianne Conner","Mitchell Cooper","Johnnie Greene",
"Johanna Mason","Kevin Adkins","Brandi Spencer","Marian Tyler",
"Chester Cross","Martin Lawrence","Jane Smith","Sara Jones"};
length = sizeof names / sizeof *(names + 0);
printf("Type a name to be searched:> ");
if( fgets (specificName, 100, stdin) == NULL){
printf("Error!\n");
}
specificName[strcspn(specificName, "\n")] = 0;
if((search(names,specificName,length)) == 0){
printf("Found %s",specificName);
}else{
printf("There was no Record Found.\n");
}
return 0;
}
Output:
Type a name to be searched:> Sara Jones
Found Sara Jones
Took a quick look and it seems to work fine. Your i needs to be declared as an int but otherwise, the following works:
#include <stdio.h>
#include <stdlib.h>
#define MAX_CLASS_SIZE 100
#define MAX_NAME_SIZE 100
int strcmp(const char *, const char *);
int main(int argc, const char * argv[]) {
char names [MAX_CLASS_SIZE][MAX_NAME_SIZE+1]={"Julias Hoffman","Dianne Conner","Mitchell Cooper","Johnnie Greene","Johanna Mason","Kevin Adkins","Brandi Spencer","Marian Tyler","Chester Cross","Martin Lawrence","Jane Smith","Sara Jones"};
char specificName[]="Brandi Spencer";
int search(char names[][MAX_NAME_SIZE+1],char specificName[]);
for(int i=0;i<MAX_CLASS_SIZE;i++){
if (strcmp(names[i],specificName)==0){
printf("Found %s",names[i]);
return i;
}
}
return 0;
}
Output
Found Brandi Spencer
My guess would be is that you are not passing the user input correctly. Check specificName[] prior to strcmp().
My task is to compare some words and to find a character which is not used in both of them. Here is my code. But I'm getting a warning:
[Warning] passing argument 1 of 'ret' makes pointer from integer without a cast [enabled by default].
And when I'm trying to run it it says consolepauser.exe stopped working
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char ret(char a[1][10],char b[3][10])
{
int i,j,p,t;
for (i=0;i<1;i++)
for (j=0;j<10;j++)
for (p=0;p<3;p++)
for (t=0;t<10;t++)
{
if (tolower(a[i][j]==tolower(b[p][t])))
{
p=3;
break;
}
if (p==2)
if (t==9) return tolower(a[i][j]) ;
}
return 'N';
}
int main(int argc, char *argv[]) {
char k[3][10]={"cHaOs","TOP","blAa"};
char b[1][10]={"SomeThIng"};
char q[1][10]={"HaPa"};
if (ret(b[1][10],k[3][10])='N') printf("No character") ;
else printf("%c",ret(b[1][10],k[3][10])) ;
return 0;
}
You should pass the parameters as:
if (ret(b, k) == 'N') printf("No character");
else printf("%c", ret(b, k));
[Warning] passing argument 1 of 'ret' makes pointer from integer without a cast
b[1][10] is a char, not a variable of type char [1][10], you should call ret() like this: ret(b, k). Others are similar.
Note: the valid indexes of char b[1][10]; are b[0][0], b[0][1], ..., b[0][9], the indexes in `b[1][10]1 are out-of-bounds, and will cause undefined behavior.
Here is a syntax fixed version of your code, you may want to compare it with your original code to find out other problems in it:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char ret(char a[1][10],char b[3][10])
{
int i,j,p,t,e,r;
for (i=0;i<1;i++)
for (j=0;j<10;j++)
for (p=0;p<3;p++)
for (t=0;t<10;t++)
{
if (tolower(a[i][j])==tolower(b[p][t]))
{
p=3;
break;
}
if (p==2)
if (t==9) return tolower(a[i][j]) ;
}
return 'N';
}
int main(int argc, char *argv[]) {
int i,j,p,t,e,r;
char a,h;
char k[3][10]={"cHaOs","TOP","blAa"};
char b[1][10]={"SomeThIng"};
char q[1][10]={"HaPa"};
if (ret(b,k)=='N') printf("No character");
else printf("%c",ret(b,k));
return 0;
}
main
...
char coords;
get_coords(1, &coords);
printf("{%s}", coords);
...
get_coords:
void get_coords(int num, char *coords){
if (num == 1){
*coords = '0-0';
}
}
problem seems to happen when the code tries to execute the printf and simply returns errors back... if i remove that line of code or change %s to %c it will work fine...
char coords;
printf("{%s}", coords);
coords is a char and not a string. %s conversion specification requires a char * argument and not a char. Passing an object of type char is undefined behavior.
%s is the format specifier for a C string. You're passing it a simple char.
What you want is probably something like this (please add the error checks yourself):
#include <stdio.h>
#include <string.h>
void get_coords(int num, char *coords)
{
if (num == 1) {
coords[0] = '0';
coords[1] = '-';
coords[2] = '0';
coords[3] = '\0';
}
}
int main(void)
{
char coords[4];
memset(coords, 0, sizeof(coords));
get_coords(1, coords);
printf("{%s}\n", coords);
return 0;
}
What's wrong with this?
#include <stdio.h>
void main(){
char *s="some text";
printf("%d",is_in(s,'t'));
}
int is_in(char *s, char c){
while(*s){
if(*s==c) return 1;
s++;
}
return 0;
}
I get the following compile time error with GCC:
test.c:9: error: conflicting types for ‘is_in’
test.c:9: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
test.c:5: note: previous implicit declaration of ‘is_in’ was here
Have you tried putting the is_in function above main?
You are incrementing the character, not the pointer. Change *s++ to simply s++. In addition, you have forgotten to forward-declare your function "is_in". One other note: you should probably make your string a "const char*" instead of "char*", and, IMHO, explicit comparison with '\0' and using indexes is clearer:
#include <stdio.h>
int is_in(const char*, char);
int main(int argc, char* argv[]){
const char* str="some text";
printf("%d",is_in(s,'t'));
return 0;
}
int is_in(const char* str, char c){
int idx=0;
while ( str[idx] != '\0' ){
if ( str[idx] == c ){
return 1;
}
idx++;
}
return 0;
}