I'm a beginner in C language. After reading the initial chapters of Ritchie's book, I wrote a program to generate random numbers and alphabets.
The program compiles fine with gcc. However on running it, it gives an error "Segmentation fault", which is incomprehensible to my limited knowledge. I'd be glad to understand what I've written wrong.
#include <stdio.h>
#include <stdlib.h>
#include "conio.h"
#include <time.h>
long int genrandom(int,int);
void randAlph(void);
char letterize(int);
int main (void) {
// char full[9];
// char part_non[4];
srand(time(0));
int i;
for (i=0;i<50;++i) {
randAlph();
};
}
long int genrandom(int mino,int maxo) {
int val=mino+rand()/(RAND_MAX/(maxo-mino)+1);
return val;
}
void randAlph (){
int val;
char text;
val=genrandom(0,26);
// return val;
text=letterize(val);
printf("%s ,",text);
}
char letterize(int num) {
char letter='A'+num;
return letter;
}
printf("%s ,",text); is wrong - it says that text is a nul-terminated array of chars. Use
printf("%c ,", text);
instead to print your single char.
#include <stdio.h>
#include <stdlib.h>
#include "conio.h"
#include <time.h>
int genrandom(int,int);
void randAlph(void);
char letterize(int);
int main (void) {
// char full[9];
// char part_non[4];
srand(time(0));
int i;
for (i=0;i<50;++i) {
randAlph();
};
}
int genrandom(int mino,int maxo) {//changed function return type to int
int val=mino+rand()/(RAND_MAX/(maxo-mino)+1); //Be careful when you are using '/' operator with integers
return val; //returning int here why set return type to long int?
}
void randAlph (){
int val;
char text;
val=genrandom(0,26);
// return val;
text=letterize(val);
printf("%c ,",text);//Replace %s with %c
}
char letterize(int num) { //No bound checking on num eh?
char letter='A'+num;
return letter;
}
That's all I had to say. :)
Why use %s when text is char. You dont need a string type in the function. Just a char would do. Change in the function : void randAlph ()
printf("%s ,",text);
to
printf("%c ,", text);
Related
I know I should not use c and c++ at the same time.
Can someone say why the above code are working using new?
the purpose is to remove the central character of an word given by keyboard ex: "abcde" to "abde"
I was asking if the creation of VLA is correct or not... apparently it returns what I want BUT the same main code without the other functions crashes.
I searched throw internet and i discovered that i should initialize the size ('n' in my case)of the VLA's.
Code using functions and new:
#include <stdio.h>
#include <string.h>
int citirea_sirului(char *s1, char *s2)
{
int d;
printf("Cuvantul: ");
gets(s1);
d=strlen(s1);
for(int i=0;i<d;i+=1)
{
*(s2+i)=*(s1+i);
}
return d;
}
void prelucrarea_afis_siruluiC(char *b, int d, char *a){
strcpy(a,b+(d/2)+1);
strcpy(b+(d/2),"");
strcat(b,a);
puts(b);
}
int main(){
int n;
char *cuv,*ccuv;
cuv=new char[n];
ccuv=new char[n];
n=citirea_sirului(cuv,ccuv);
printf("Dimensiunea Cuvantului: %d\n",n);
printf("\nSir prelucrat: \n");
prelucrarea_afis_siruluiC(ccuv,n,cuv);
delete[] ccuv;
delete[] cuv;
return 0;
}
Code without functions:
#include <stdio.h>
#include <string.h>
int main(){
int n;
char cuv[n], ccuv[n];
printf("Cuvantul: ");
gets(cuv);
n=strlen(cuv);
printf("Dimensiunea Cuvantului: %d",n);
for(int i=0;i<n;i++)
{
ccuv[i]=cuv[i];
}
strcpy(cuv,cuv+(n/2)+1);
strcpy(ccuv+(n/2),"");
strcat(ccuv,cuv);
printf("\nCuvantul prelucrat: %s",ccuv);
return 0;
}
This question already has answers here:
returning a local variable from function in C [duplicate]
(4 answers)
Changing address contained by pointer using function
(5 answers)
Closed 1 year ago.
I am trying to implement a function as stated in the title. I think I am very close to solution but a problem.
input: 51% are admitted.
output: x:51 (null)
but output should have been:
s:% are admitted.
My try is here:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int str2int(int);
int isdigit(int);
long str2double(int);
int driver(char *, char *);
int main(){
char *s = "51% are admitted.";
char *sPtr;
int x = driver(s, sPtr);
printf("x:%d sPtr:%s", x, sPtr);
return 0;
}
int isdigit(int ch){
return (ch>=48 && ch<=57)?1:0;
}
int str2int(int ch){
return ch-48;
}
int driver(char *s, char *sPtr){
int i=0, number=0;
while(s[i]!='\0' && isdigit(s[i])){
number = number*10 + str2int(s[i]);
i++;
}
sPtr=s+i;
printf("%s\n", sPtr);
return number;
}
The problem is, in main, sPtr seems as null but in driver function, sPtr is % is admitted which is what it should be. How can I fix the problem so that I can print the solution correctly without using a printf statement in driver function?
EDIT:
The problem is as #Johnny Mopp said, I was trying to pass a copy of that variable. Therefore, I need to pass the address of variable of *sPtr which appears char **sPtr in prototype. And the code should be:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int str2int(int);
int isdigit(int);
long str2double(int);
int driver(char *, char **);
int main(){
char *s = "51% are admitted.";
char **sPtr;
int x = driver(s, &sPtr);
printf("x:%d sPtr:%s", x, sPtr);
return 0;
}
int isdigit(int ch){
return (ch>=48 && ch<=57)?1:0;
}
int str2int(int ch){
return ch-48;
}
int driver(char *s, char **sPtr){
int i=0, number=0;
while(s[i]!='\0' && isdigit(s[i])){
number = number*10 + str2int(s[i]);
i++;
}
*sPtr=s+i;
return number;
}
Thanks for contributes of #Johnny Mopp and #paulsm4
I am trying to run below program in an online C compiler. But I get segmentation error. Can you help me fix this
#include <stdio.h>
#include <string.h>
int main()
{
char string[15] = "Strlwr in C";
printf("%s",tolower(string));
return 0;
}
Following is the prototype of tolower
int tolower(int c);
You should pass an int or something like char which can safely convert to int. Passing char * (Type of string) like you do leads to UB.
To convert a string to lowercase, you need to convert each character separately. One way to do this is:
char string[15] = "Strlwr in C";
char lstr[15];
int i = 0;
do {
lstr[i] = tolower(string[i]);
} while(lstr[i] != '\0');
printf("%s", lstr);
You are using tolower incorrectly. This function returns int and gets int as a parameter (here is it's declaration: int tolower(int c);). What you want to do is call it on each char of your char array, and print each one:
char string[15] = "Strlwr in C";
for(int i = 0; i < strlen(string); i++)
printf("%c",tolower(string[i]));
Read cplusplus.com/reference/cctype/tolower It takes a single int as parameter, not char and not array.
You probably want to use a loop on "string", which processes each in turn.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
int i;
char string[15] = "Strlwr in C";
for (i=0; i< sizeof(string)/sizeof(char); i++)
{
string[i]=(char)(tolower((int)string[i]));
}
printf("%s\n",string);
return 0;
}
Output:
strlwr in c
This function stops awsering, and I can't spot the prob, can someone try to find it please?
It is suposed to give me the designed name and number from an array of strings.
#include <stdio.h>
#include <string.h>
int sameName();
char **getNumber();
char **getNumber (char *n[], char e[],int N){
int a;
for(a=0;a<N;a++){
if (sameName(n[a],e))
{
return n[a];
}
}
return "Not found!";
}
int sameName(char n[], char e[]){
int a;
for(a=0;e[a]!='\0';a++){
if (n[a]!=e[a])
{
return 0;
}
}
return 1;
}
int main (){
char numbers [5] [100] ={{"Ash 031"},{"Bomberman 021"},{"Rango 120"},{"Gigo Senhas 017"},{"Marcoreano 135"}};
char name [100];
char a [100];
scanf("%s",&a);
strcpy(name,getNumber (numbers,a,5));
printf("%s\n",name);
return 0;
}
You need to spicify the size of your two-dimension array when you pass it to your getNumber function. See http://c-faq.com/aryptr/pass2dary.html for details.
And for scanf you only need to pass a since it is a string.
Taking these plus fixing your prototypes as mentioned in the comment above, that gives :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sameName(char n[], char e[]);
char *getNumber (char n[][100], char e[],int N);
char *getNumber (char n[][100], char e[],int N){
int a;
for(a=0;a<N;a++){
if (sameName(n[a],e))
{
return n[a];
}
}
return "Not found!";
}
int sameName(char n[], char e[]){
int a;
for(a=0;e[a]!='\0';a++){
if (n[a]!=e[a])
{
return 0;
}
}
return 1;
}
int main (){
char numbers [5] [100] ={{"Ash 031"},{"Bomberman 021"},{"Rango 120"},{"Gigo Senhas 017"},{"Marcoreano 135"}};
char name [100];
char a [100];
scanf("%s",a);
strcpy(name,getNumber (numbers,a,5));
printf("%s\n",name);
return 0;
}
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;
}