C Programming about choosing my version of a function - c

In function main() {...}
1) #include header file string.h
2) I prototype my own file, call it strcpy:
**char *strcpy(char *strSource , const char *dest);**
3) I also wish to use the "real" strlen function in string.h in main().
4) In another compilation file I have my version of strcpy.
Question: How can I get the linker to choose my version of strcpy instead of the prototype in string.h?
enter code here
#include <conio.h>
#include <string.h>
char *strcpy(char *source , const char *dest);
void main()
{
char *s, *d;
strcpy(s,d);
getch();
}
#include <stdio.h>
char *strcpy(char *strDestination, const char *strSource)
{
char *ptr;
printf("made it!");
return ptr;
}

You can't do that in C. That's one of the reasons why C++ introduces namespaces.
The only thing you can do is use a non-conflicting name for your own functions.

This is not a standard thing to do, and I'm pretty sure that if you can do it at all, you will have to read the manual for your linker. And if you try to port this, you will need to read the manual for each linker for each platform to which you try to port this.
The more usual way to do things is to write your own functions with your own names. If you need to hook functionality in a standard library, you would get the source for that library and build your own version.

Related

Using struct and strcpy, program crashes

Hello this is my first time posting on this site and also I am not very familiar with structures or with strcpy() I was wondering why my program below is crashing.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
struct Employee{
char name[30];
char email[30];
};
void main(){
struct Employee x;
char employee_name[30];
char employee_email[30];
printf("enter the employees's name\n");
fgets(employee_name,30,stdin);
strcpy(x.name, employee_name);
printf("enter the employee's email\n");
fgets(employee_email,30,stdin);
strcpy(x.email,employee_email);
printf('%s',x.name);
printf('%s',x.email);
}
The purpose of the program is basically to accept a name and email as input and put it in the name and email of the structure and than print them using the structure. Now the program compiles and allows me to take the input but after that it crashes and I do not know why. Does anyone know why the crash is occurring?
The issue is with
printf('%s',x.name);
printf('%s',x.email);
as per the printf() format,
int printf(const char *format, ...);
the fisrt argument is a const char *. So, you need to write
printf("%s",x.name);
printf("%s",x.email);
That said,
void main() should be int main(void), at least to conform to the standards.
fgets() scans and stores the trailing newline (if any) to the input buffer as a part of the input. You may want to strip it off before copying the buffer.

When using readline() function, why is it necassary to pass NULL as its argument?

In C programming why do you pass NULL as a argument when using the readline() function?
Is there any other way to store the name beside using const char?
#import <readline/readline.h>
#import <stdio.h>
int main(int argc, const char * argv[])
{
printf("Who is cool? ");
const char *name = readline(NULL);
printf("%s is cool!\n\n",name);
return 0;
}
Edit: when using this code (in Xcode) I noticed the characters I typed was echoed back.
SO what I see from the console was actually like so:
who is cool? MMiikkeeyy
Mikey is cool!
Is there an explanation to why this happens?
The readline function is a GNU thing, allowing for user input with all the editing power found in various shells.
The argument is simply a prompt to issue before asking for input, so your code is equivalent to (in C code since my Mac is not handy):
#include <stdio.h>
#include <readline/readline.h>
int main (int argc, const char * argv[]) {
const char *name = readline ("Who is cool? "); // Don't need separate printf.
printf ("%s is cool!\n\n", name);
return 0;
}
Further details on how to use it can be found here, one of the pages referenced by the link in the first sentence.
You Pass NULL as an argument when using the readline() function because the compiler should know in advance that there is no line written there before any execution. Hence avoiding this type of error we use NULL .
And You Can store your characters using pointers and arrays in c.

Strange thing about pointers

I have some misunderstanding about VS C++. In version 2010 the code below works fine: I can get a string and I can free memory afterwords.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAX 14
void GetString(char **str);
int main(int argc, char *argv[])
{
char *str = NULL;
GetString(&str);
printf("%s\n", str);
getchar();
free(str);
return 0;
}
void GetString(char **str)
{
char *s = (char *) malloc(sizeof(char) * MAX);
strcpy(s, "HELLO, WORLD!");
*str = s;
}
But in VS 2008 the code above will end up with memory corruption error. I guess, there is a small difference in standards used. Am I right? If no, could you, please, tell me, why the same code doesn't work in different versions of the Visual Studio?
Thank you beforehand for your answers.
p.s. I'm curious of what happens, but couldn't yet find any information on the topic.
p.p.s. Language used - C
You're not including the required headers, which means your code could be interpreted differently by different compilers. You should add:
#include <stdlib.h>
to make malloc() a well-defined function.
You're also calling strcpy(), so you need:
#include <string.h>
and doing I/O, for which you need:
#include <stdio.h>
Also, in C this:
char *s = (char *) malloc(sizeof(char) * MAX);
is better written as:
char *s = malloc(MAX);
since
It's a bad idea to cast the return value of malloc()
sizeof (char) is always 1, so it just adds clutter.
Finally, you should of course check that malloc() succeeds before using the returned pointer.

C functions without header files

This should be very trivial. I was running through a very basic C program for comparing strings:
#include <stdio.h>
int strcmp(char *s, char *t);
int main()
{
printf("Returned: %d\n", strcmp("abc", "adf"));
return 0;
}
int strcmp(char *s, char *t)
{
printf("Blah\n");
while (*s++ == *t++)
{
if (*s == '\0')
return 0;
}
return *s - *t;
}
So I've basically implemented my own version of the strcmp function already present in string.h. When I run the above code, I only see return values of 0, 1, or -1 (at least for my small set of test cases) instead of the actual expected results. Now I do realize that this is because the code doesn't go to my implemented version of strcmp, but instead uses the string.h version of the function, but I'm confused as to why this is the case even when I haven't included the appropriate header file.
Also, seeing how it does use the header file version, shouldn't I be getting a 'multiple implementations' error (or something along those lines) when compiling the code?
You're using gcc, right? gcc implements some functions as built-ins in the compiler and it seems that strcmp is one of those. Try compiling your file with the -fno-builtin switch.
Header files just tell the compiler that certain symbols, macros, and types exist. Including or not including a header file won't have any effect on where functions come from, that's the linker's job. If gcc was pulling strcmp out of libc then you probably would see a warning.
Not as elegant as the earlier answers, another way to get this done
#include <stdio.h>
static int strcmp(char *s, char *t); /* static makes it bind to file local sym */
int main()
{
printf("Returned: %d\n", strcmp("abc", "adf"));
return 0;
}
int strcmp(char *s, char *t)
{
printf("Blah\n");
while (*s++ == *t++)
{
if (*s == '\0')
return 0;
}
return *s - *t;
}
Without knowing what compiler and lib. version you use, all conclusion are only 'possibility'. So the most reasonable thing that stdio.h already includes stdlib.h or string.h
strcmp is the name of a standard library function. As such you are only permitted to declare the function (although you must use a correct declaration); you are not permitted to provide another definition for it. The implementation can assume that whenever you use strcmp you are referring to the standard library function even if you haven't used the correct #include for it.
If you want to provide an alternative strcmp then you should give it an alternative name.

how to assign a value to a string array?

for example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char substr[10][20];
int main() {
substr[0] = "abc";
printf("%d", substr[0]);
}
of course above is wrong? how to do it? thanks
You can't assign strings like that in C. Instead, use strcpy(substr[0], "abc"). Also, use %s not %d in your printf
I hate giving 'full' answers to homework questions, but the only way to answer your question is to show you the answer to your problem, because it is so basic:
#include <stdio.h>
#include <string.h>
#define MAXLEN 20
char substr[10][MAXLEN];
int main(void) {
strncpy(substr[0], "abc", MAXLEN);
puts(substr[0]);
return 0;
}
Your code (as is) has several faults:
You are treating substr[0] as a string literal. You can't do that.
You were using printf formatters incorrectly. %s is for strings
You don't need to bother printf() to print a string
You should (in real code) watch out for buffer overflows, hence strncpy()
If main() doesn't want / need argc and argv, its arguments should be void
main() should return a value, as its return type is int
You aren't using anything out of <stdlib.h>, why include it?
I suggest researching string literals, the functions available in <string.h> as well as format specifiers.
Also note, I am not checking the return of strncpy(), which is something you should be doing. That is left as an exercise for the reader.
Hope this helps:
void main(void)
{
char* string[10];
string[0] = "Hello";
}
Otherwise I think ya need to copy it by hand or use strcpy or the like to move it from one block of memory to another.

Resources