This question already has an answer here:
Reversing a string literal in C with pointers [duplicate]
(1 answer)
Closed 4 years ago.
so this is the code for reversing a string
#include<stdio.h>
char* function(char *);
int main()
{
char a[]="computer";
printf("%s", function(a));
return 0;
}
char* function(char *p)
{
int l,i;
char t;
for (l=0;*(p+l)!='\0';l++);
for(i=0; i<(l/2) ; i++)
{
t=*(p+i);
*(p+i)=*(p+l-1-i);
*(p+l-1-i)=t;
}
return (p);
}
but if i change printf("%s", function(a)); in the main body to
printf("%s", function("computer"));
there is no output (the output is blank) in dev c++.... but it gives the desired output in turbo c++ even with this change....why is that?
Parameter "computer", which you pass to function, is a string literal, and changing/manipulating the contents of a string literal is undefined behaviour. That's what you are experiencing - something undefined.
Related
This question already has answers here:
Since I can't return a local variable, what's the best way to return a string from a C or C++ function?
(8 answers)
Return a string from function to main
(3 answers)
How to access a local variable from a different function using pointers?
(10 answers)
Closed 3 years ago.
I kept getting the error "Command terminated by signal 11" when I try to save a string into an array of strings. After changing the code from the link below, now the name prints out ���W.
I used this link to fix my code off of but it is still causing me to get the error "Command terminated by signal 11".
#include<stdio.h>
#include<string.h>
void getName(char* c[], int size);
int main(void) {
int yenoSize = 0;
int strSize = 0;
char name1[30];
char* name[30];
char* students[5][30];
for(int i=0;i<1;i++){
getName(name, strSize);
students[i][0] = name1;
}
for(int k=0; k<1;k++){
printf("Student Name: %s \n", students[k][30]);
}
}
void getName(char* c[], int size){
char name1[30];
printf("Enter student name: ");
fgets(name1, 30, stdin);
c[0] = &name1[0];
printf("%s", name1);
}
The output is supposed to print out the name that the user inputs (Student Name: Jon) but it is currently printing ���W. How can I fix the problem? I believe the problem exists with the name pointer pointing to null. Is that the problem? I appreciate the help!
2 major issues.
1) In main(), you are calling getName() with name but storing name1 in students variable.
2) In function getName(), name1 is a local array. The scope of this variable is limited to getName(). You can not return address of name1 by assigning it to c[0] as it may get freed by OS after function returns. You can consider using malloc() for this.
This question already has answers here:
Returning a C string from a function
(15 answers)
Closed 4 years ago.
I have to make a function that has a float as parameter, and the function will return that number in words (As a string)
i.e: function(12.0) will return twelve as a string
But I have some problems returning the string, it returns some weird data, I have this code:
char* function (float number)
{
char str[] = "test";
strcat(str, " char");
return str;
}
int main()
{
char *test = function(5.0);
printf("Hi %s", test);
}
That always returns a different thing:
Hi u.ª╣D
Hi uêõWÛ
I don't know how to fix it, because I really need to use strcat to do things like 'one thousand ten' saving time and lines of code, and it's not possible if I define char str as a pointer...
I'm using Dev-C++ 5.11
Thank you!
I searched a few days but noone of the answers worked with what I need, because they define the string as a pointer or use more than 1 parameter in the function (it only has to receive 1 float parameter, no more parameters are accepted)
str is local to function and will be destroyed once control exits function.
Hence you can try as below.
char* function (float number)
{
char *str = malloc(30);
strcpy(str, " test");
strcat(str, " char");
return str;
}
int main()
{
char *test = function(5.0);
printf("Hi %s", test);
}
This question already has answers here:
What is a segmentation fault?
(17 answers)
Closed 4 years ago.
Recently i have started working on built-in functions but came up with an error and that is:
Why am i getting segmentation fault for this program
#include<stdio.h>
#include<ctype.h>
int main()
{
char str[50];
int n;
printf("Who is your best friend? ");
scanf("%s",str);
n=isalpha(str);
if(n!=0)
{
printf("Is Alpha");
}
else
{
printf("Invalid Input");
}
return 0;
}
Please help me out...
isalpha()'s prototype is
int isalpha( int ch );
The argument is of type int. But the one that you are passing is of type char * since str is a character array.
Perhaps you meant
unsigned char str;
scanf("%c",&str);
isalpha() returns 0 if its argument is not alphabetic.
And to avoid overflow, you could modify your scanf() to
scanf("%49s",str);
with one character to store the \0 character.
Have a look at this post.
Edit: The argument of isalpha() shouldn't be char. It must be at least unsigned char as explained here. Thanks to melpomene for pointing this out.
This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 6 years ago.
This is what I'm trying to do but my code is either not compiling or giving me an unexpected output "BC" instead of just "B".
#include <stdio.h>
void removeFirstAndLastChar(char** string) {
*string += 1; // Removes the first character
int i = 0;
for (; *string[i] != '\0'; i++);
*string[i - 1] = '\0';
}
int main(void) {
char* title = "ABC";
removeFirstAndLastChar(&title);
printf("%s", title);
// Expected output: B
return 0;
}
I looked through a lot of answers here related to passing pointers by reference but none of them seemed to contain the operations that I want to do in my removeFirstAndLastChar() function.
I do not judge your algorithm or C conventions, friends who comment on your problem are totally right. But if you still do it in this way you can use this approach.
#include <stdio.h>
#include <string.h>
void removeFirstAndLastChar(char* string) {
memmove(string,string+1,strlen(string));
string[strlen(string)-1]=0;
}
int main(void) {
char title[] = "ABC";
removeFirstAndLastChar(title);
printf("%s", title);
// Expected output: B
return 0;
}
This question already has answers here:
why this program doesn't give the expected output? [duplicate]
(2 answers)
Closed 7 years ago.
I'm stuck here trying to understand why this assignement can't work in this way in C. What I'm trying to do is substitute all space occurrences with underscore char. (output: Hi_from_Synchronyze)
I saw that the problem comes when I try to do this..
s[n]='_';
the complete code is this one
#include <stdio.h>
#include <stdlib.h>
char *underscore(char *s, int n);
int main()
{
printf("%s", underscore("Hi from Synchronyze", 0));
return 0;
}
char *underscore(char *s, int n)
{
if(s[n]=='\0')
return s;
else {
if(s[n]==' ') {
s[n]='_';
return underscore(s, n+1);
}
else return underscore(s, n+1);
}
}
I'd like to know what's going on behinde and why this happens, not the solution.
Thank you very much in advance
String literals are read-only, so you can't assign to them.
Make a mutable copy of the string first, something like:
char text[] = "Hi from Synchronyze";
printf("%s", underscore(text, 0));