I'm totally new in C and I want to write a function :
#include <unistd.h> //import write...
void ft_putchar(char str[20]) {
write(1, &str, 19);
}
char main() {
char str2[20] = "GeeksforGeeks";
ft_putchar(str2[20]);
return(0);
}
Hope this will help you a bit :)
#include <unistd.h> //import write...
void ft_putchar(char *str) { // the size of the buffer is not required
write(1, str, strlen(str)); // 2nd argument is a char*, not a char **
// 3rd one is the actual length of your
// string, not the size of the buffer
}
int main() {
char str2[20] = "GeeksforGeeks";
ft_putchar(str2); // the size of the buffer is not required
return(0);
}
Related
I need to use sprintf() to do padding space character in string, however, I hope that the length of the padding can be changed with the length of the string.
The sample code function pcMsgPadding, I want to change left-justify width in sprintf, width depends on iLen.
It’s now a fixed width of 20.
What should i do or is there any other way?
Sample Code:
#define LCD_COLUMNS 20
char *pcMsgPadding(int iLen, const char* pcMsg)
{
char *pcBuf = (char*) malloc(LCD_COLUMNS*sizeof(char));
sprintf(pcBuf, "%-20s", pcMsg);
return pcBuf;
}
void vDisplay(const char* pcMsg)
{
printf(pcMsg);
}
void main()
{
vDisplay(pcMsgPadding(15, "Test Message"));
}
Change it to sprintf(pcBuf, "%*s", iLen, pcMsg);.
The whole program with some problems fixed (added includes, removed the malloc cast, you should not use a variable as first parameter in printf, main must return int):
#include <stdio.h>
#include <stdlib.h>
#define LCD_COLUMNS 20
char *pcMsgPadding(int iLen, const char* pcMsg)
{
char *pcBuf = malloc(LCD_COLUMNS*sizeof(char));
sprintf(pcBuf, "%*s", iLen, pcMsg);
return pcBuf;
}
void vDisplay(const char* pcMsg)
{
printf("%s", pcMsg);
}
int main(void) {
vDisplay(pcMsgPadding(15, "Test Message"));
return 0;
}
I'm confusing about the different sizeof() return value after function check() calls. And buf exactly the same as buffer based on the printf() for each char. Any reply is awesome! Thx.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void check(char *buf)
{
printf("%d \n", sizeof(buf)); // **output 8**
}
int main(int argc, char *argv[])
{
char buffer[] = "\x64\x49\x00\x55\x33\x33\x33\x64\x49\x00\x00\x00\x55\x33\x33\x33\x64\x49\x00\x00\x00\x55\x33\x33\x64\x49\x00\x00\x00\x55\x33\x33";
printf("%d\n", sizeof(buffer)); // **output 33**
check(buffer);
}
In function check buffer is a pointer to the char and its size is 8
In the main furnctions buffers is the 33 elements char array and its size is 33
To get the length of the C string use strlen function.
Generally there is no way of getting the size of the array referenced by the pointer. You need to pass the size as a anothother parameter.
In your example:
void check(char *buf, size_t size)
{
printf("%zu \n", size);
}
int main(int argc, char *argv[])
{
char buffer[] = "\x64\x49\x00\x55\x33\x33\x33\x64\x49\x00\x00\x00\x55\x33\x33\x33\x64\x49\x00\x00\x00\x55\x33\x33\x64\x49\x00\x00\x00\x55\x33\x33";
printf("%d\n", sizeof(buffer)); // **output 33**
check(buffer, sizeof(buffer));
}
Its a simple input that I want to make for char*. Why is this not working?
It throws me an exception that I can't resolve..
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
char* GetCharSeq()
{
char *s = (char*)malloc(100);
scanf_s("%s", s);
return s;
}
int main()
{
char* charseq;
charseq = GetCharSeq();
return 0;
}
You have undefined behavior in your code. You have it because you provide to few arguments to the scanf_s function.
For every string argument, you need to provide not only the destination string but also the size of the string. So change your call to
scanf_s("%s", s, 100);
modify your code
char* GetCharSeq()
{
char *s = (char*)malloc(100);
gets(s);
return s;
}
This will work.
Here's the code:
#include <stdio.h>
#include <string.h>
void print (void*);
int main (void)
{
char *a = "Mcwhat";
print(&a);
printf("\n%s", a);
return 0;
}
void print (void *text)
{
char* pchar[5];
*pchar = (char*)text;
strcpy( *pchar, "Mcthat" );
}
I am trying to make Mcwhat into Mcthat using a void parameter, but the printf gives me a segmentation fault afterwards. Where is my mistake? I managed to do it char by char but now I want to change the whole string. Didn't found enough material on this in the books on C I have.
Keep it simple and pay attention to the type of your variables :
#include <stdio.h>
#include <string.h>
void print (void*);
int main()
{
char a[] = "Mcwhat"; // a is now a read-write array
print(a); // a decays to a pointer, don't take its adress or you'll get a pointer-to-pointer
printf("\n%s", a);
return 0;
}
void print (void *text)
{
strcpy( text, "Mcthat" ); // Don't dereference text here
}
Note that this "print" function is unsafe in all imaginable ways, but that wasn't the question.
There are lot of issues in your code:
1. Char array should be big enough to store the string. char[5] cannot hold Mswhat.
2. char* pchar [5] declares 5 char pointers, whereas you need one char pointer pointing to a char array.
I have corrected it.
#include <stdio.h>
#include <string.h>
void print (char*);
int main (void)
{
char *a = malloc(10);
strcpy(a,"Mcwhat");
print(a);
printf("\n%s", a);
free(a);
return 0;
}
void print (char *text)
{
char *pchar = text;
strcpy( pchar, "Mcthat" );
}
Just write it like that
void print (char *text)
{
strcpy( text, "Mcthat" );
}
But make sure, the that size of text is large enough to put "Mcthat" inside it.
Also in main:
print(a);
instead of
print(&a); // would requite void print (char** text)
tho whole shebang:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void print (void*);
int main (void)
{
char *a = malloc(strlen("Mcwhat")+1);
print(a);
printf("\n%s\n", a);
free(a);
return 0;
}
void print (void *text)
{
strcpy(text, "Mcthat" );
}
I want to return a string from a function (in the example funzione) to main. How to do this? Thank you!
#include <stdio.h>
#include <string.h>
#define SIZE (10)
/* TODO*/ funzione (void)
{
char stringFUNC[SIZE];
strcpy (stringFUNC, "Example");
return /* TODO*/;
}
int main()
{
char stringMAIN[SIZE];
/* TODO*/
return 0;
}
[EDITED] For those who need it, the complete version of the previous code (but without stringMAIN) is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE (10)
char *funzione (void)
{
char *stringa = malloc(SIZE);
strcpy (stringa, "Example");
return stringa;
}
int main()
{
char *ptr = funzione();
printf ("%s\n", ptr);
free (ptr);
return 0;
}
A string is a block of memory of variable length, and C cannot returns such objects (at least not without breaking compatibility with code that assumes strings cannot be returned)
You can return a pointer to a string, and in this case you have two options:
Option 1. Create the string dynamically within the function:
char *funzione (void)
{
char *res = malloc (strlen("Example")+1); /* or enough room to
keep your string */
strcpy (res, "Example");
return res;
}
In this case, the function that receives the resulting string is responsible for deallocate the memory used to build it. Failure to do so will lead to memory leaks in your program.
int main()
{
char *str;
str = funzione();
/* do stuff with str */
free (str);
return 0;
}
Option 2. Create a static string inside your function and returns it.
char *funzione (void)
{
static char str[MAXLENGTHNEEDED];
strcpy (str, "Example");
return str;
}
In this case you don't need to deallocate the string, but be aware that you won't be able to call this function from different threads in your program. This function is not thread-safe.
int main()
{
char *str;
str = funzione();
/* do stuff with str */
return 0;
}
Note that the object returned is a pointer to the string, so on both methods, the variable that receives the result from funzione() is not a char array, but a pointer to a char array.
#include <stdio.h>
#include <string.h>
#define SIZE 10
const char *funzione (void){
const char *string = "Example";
if(strlen(string) >= SIZE)
return "";
return string;
}
int main(void){
char stringMAIN[SIZE];
strcpy(stringMAIN, funzione());
printf("%s", stringMAIN);
return 0;
}
You can do this as
char *funzione (void)
{
char *stringFUNC = malloc(SIZE);
strcpy (stringFUNC, "Example");
return stringFUNC;
}
In main, call it as
int main()
{
char stringMAIN[SIZE];
char *ptr = funzione ()
...
free(ptr);
return 0;
}