Cannot make function read Double array - arrays

So i have a piece of code where i want to store 5 random names no longer than 10 letters each
void printname(char *s);
int main() {
char NAME[10][5];
int NAMECOUNTER=0;
while(NAMECOUNTER<5) {
scanf("%s",NAME[NAMECOUNTER]);
printname(&NAME[NAMECOUNTER]);
NAMECOUNTER++;
}
}
void printname(char *s) {
printf("Hello %s\n",*s);
return;
}
And lets say i want the name to print itself through function Printname. Why does this not work and prints "Hello (null)"?

One problem I see
char NAME[10][5];
Should be
char NAME[5][11];
This way you're declaring 5 name slots, each with 10 char max. The extra space is for the string null terminator.

You have
void printname(char *s) {
printf("Hello %s\n",*s);
return;
}
and you need
void printname(char *s) {
printf("Hello %s\n",s); // Took off the * from the *s
return;
}
because s is already your pointer.

I give 6 glitches to fix. See comments prefacing #<num>
#include <stdio.h>
#include <stdlib.h>
void printname(char *s);
int main() {
char NAME[5][11] = {0}; // #1 value initialize to zero
// #2 the second dimension should be 11, since 10 letters plus null-character
int NAMECOUNTER=0;
while(NAMECOUNTER<5) {
scanf("%10s",NAME[NAMECOUNTER]); // #3 %10s width specifier, limit the length of name up to 10 character long
while ( (ret = getchar()) != '\n') ; // #4 trim exceeding characters
printname(NAME[NAMECOUNTER]); // #5 no &
NAMECOUNTER++;
}
}
void printname(char *s) {
printf("Hello %s\n",s); // #6 no *
return;
}

Your code doesn't even print anything to me, it just freeze and closes. Also it's not so clear.
You are mistyping the "NAME" array inside printname. This function does not have a return value, since it has a void return, so you shouldn't type any return. Also you can save some code lines writing it before main function which is the best practice:
void printname(){
//todo
}
int main(){
return 0;
}
This is a better way to implement your function:
void printname(char Names[5][11], int index){
printf("Hello %s\n", Name[index]);
}
You could replace the while structure with for structure, since it is the best application for the purpose in that case:
Your code piece:
while(NAMECOUNTER<5) {
scanf("%s",NAME[NAMECOUNTER]);
printname(&NAME[NAMECOUNTER]);
NAMECOUNTER++;
}
You don't have to put "&" in the first argument of printname.
Best:
for(NameCounter = 0; NameCounter < 5; NameCounter++){
scanf("%s",NAME[NAMECOUNTER]);
printname(NAME[NAMECOUNTER]);
}
Also make sure to keep your code indentation clear. This is your final code:
void printname(char Names[][11], int index){
printf("Hello %s\n", Names[index]);
}
int main(){
char NAME[5][11];
int NAMECOUNTER;;
for(NAMECOUNTER = 0; NAMECOUNTER < 5; NAMECOUNTER++){
scanf("%s", NAME[NAMECOUNTER]);
printname(NAME, NAMECOUNTER);
}
}

Related

Stack smashing detected in C - why does this happen?

I have the following function, which, given a string, should find the most recurrent couple of letters in it and store the result in a different string.
For example - for the string "ababa", the most recurrent couple would be "ba", and for "excxexd" it would be "ex". This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
void printError(){
printf("Error: please check your input\n");
}
bool isLexicographicallyPreceding(char couple1[], char couple2[])
{
if (strcmp(couple1, couple2)>=0) return true;
return false;
}
void coupleDetector(int length, char word[], char result[])
{
char couples[length-1][2];
for (int i=0; i<length-1; i++)
{
char couple[2] = {word[i], word[i+1]};
strcpy(couples[i], couple);
}
char element[]="";
int count=0;
for (int j=0; j<length-1; j++)
{
char tempElement[2];
strcpy(tempElement,couples[j]);
int tempCount=0;
for (int p=0; p<length-1; p++)
{
if (couples[p]==tempElement) tempCount++;
}
if (tempCount>count)
{
strcpy(element, tempElement);
count=tempCount;
}
if (tempCount==count)
{
if (isLexicographicallyPreceding(tempElement,element) == true) strcpy(element, tempElement);
}
}
strcpy(result,element);
}
int main() {
//Supposed to print "ba" but instead presents "stack smashing detected".
int length=5;
char arr[] = "ababa";
char mostCommonCouple[2];
coupleDetector(length,arr,mostCommonCouple);
printf("%s", mostCommonCouple);
return 0;
}
The code compiles without errors, but for some reason does not work as intended but prints out "stack smashing detected". Why would that be? Advices would be very helpful.
Thanks.
In trying out your program, I found a few of your character arrays undersized. Character arrays (strings) need to be sized large enough to also include the null terminator value in the array. So in many locations, having a two-character array size is not sufficient and was the cause of the stack smashing. With that in mind, following is a refactored version of your program.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
void printError()
{
printf("Error: please check your input\n");
}
bool isLexicographicallyPreceding(char couple1[], char couple2[])
{
if (strcmp(couple1, couple2)>=0) return true;
return false;
}
void coupleDetector(int length, char word[], char result[])
{
char couples[length-1][3];
for (int i=0; i<length-1; i++)
{
char couple[3] = {word[i], word[i+1], '\0'};
strcpy(couples[i], couple);
}
char element[3]; /* Define the character array */
strcpy(element, ""); /* Then initialize it if need be */
int count=0;
for (int j=0; j<length-1; j++)
{
char tempElement[3];
strcpy(tempElement,couples[j]);
int tempCount=0;
for (int p=0; p<length-1; p++)
{
if (couples[p]==tempElement) tempCount++;
}
if (tempCount>count)
{
strcpy(element, tempElement);
count=tempCount;
}
if (tempCount==count)
{
if (isLexicographicallyPreceding(tempElement,element)) strcpy(element, tempElement);
}
}
strcpy(result,element);
}
int main()
{
//Supposed to print "ba" but instead presents "stack smashing detected".
int length=5;
char arr[] = "ababa";
char mostCommonCouple[3]; /* Notice size requirement to also contain the '\0' terminator */
coupleDetector(length,arr,mostCommonCouple);
printf("%s\n", mostCommonCouple);
return 0;
}
Here are some key points.
Viewing the code, most sizes for arrays was enlarged by one to accommodate storage of the null terminator.
Work fields such as "element" need to be defined to their proper size so that subsequent usage won't also result in stack smashing.
Testing out the refactored code resulted in the following terminal output.
#Vera:~/C_Programs/Console/Recurrent/bin/Release$ ./Recurrent
ba
So to reiterate, be cognizant that character arrays normally need to be defined to be large enough to contain the largest expected string plus one for the null terminator.
Give that a try and see if it meets the spirit of your project.

Passing a string with more than one word into a function into C

Im trying to pass a string like "Hello World" into a a function that looks at each character and prints it (as a base line function for something else) I looked up how to do so and read this post pass char array as argument and while it worked great for one word strings, I can't get it working for two word strings, what can I do to get it working?
#include <stdio.h>
void printer(char *string);
char string[11];
int main(){
scanf("%s", string);
printer(string);
return 0;
}
void printer(char *words) {
for (int i = 0; i < 51; i++) {
printf("%c", words[i]);
}
}
#include <stdio.h>
void printer(char * ptr);
int main()
{
char buff[] = "hello world";
printer(buff);
return 0;
}
void printer(char * ptr)
{
printf("The String is : %s",ptr);
}

Why can't I separate letters in a string?

I want to take all the letters in a string and put it in a array separately. But I am receiving some error and I could not figure out.
10 20 E:\FALL SEM 20-21\CS\C codes\Untitled3.c [Warning] passing argument 2 of 'strcpy' makes pointer from integer without a cast
My code is
#include<stdio.h>
#include<string.h>
char array[10][100],string[100];
int top=0;
void push(char elem)
{
strcpy(array[top],elem);
top++;
}
int main()
{
printf("Enter the string: \n");
fgets(string,100,stdin);
int length;
length=strlen(string);
int i=0;
while((string[i])!='\0')
{
push(string[i]);
i++;
}
printf("%d",length);
}
strcpy() is for copying strings (sequences of characters terminated by a null-character). To use that, you should make strings from the characters and pass them. Also you have to fix the type of the argument of push().
#include<stdio.h>
#include<string.h>
char array[10][100],string[100];
int top=0;
void push(const char* elem) /* use const char* to receive strings that won't be modified */
{
strcpy(array[top],elem);
top++;
}
int main(void)
{
printf("Enter the string: \n");
fgets(string,100,stdin);
int length;
length=strlen(string);
int i=0;
while((string[i])!='\0')
{
char str[2] = {string[i], '\0'}; /* create a string */
push(str); /* and push that */
i++;
}
printf("%d",length);
}

Error with the array returning through function

I need to read a word from main function and convert the characters in UCASE if the first character is LCASE and vice versa using the user defined function.I tried ways for returning the array from function but still I am lacking some core ideas. Please debug this program and explain the way it works.
#include <stdio.h>
#include <string.h>
int* low (char str)
{
int i;
for (i=1; i<strlen(str);i++)
{
if(str[i]<91)
{
str[i]=str[i]+32;
}
else
{
}
}
return &str;
}
int* high (char str[50])
{
int i;
for (i=0; i<strlen(str);i++)
{
if(str[i]>91)
{
str[i]=str[i]-32;
}
else
{
}
}
return &str;
}
void main()
{
char str[50];
char* strl;
printf("Enter any string....\n");
scanf("%s",str);
if (str[0]<91)
{
*strl=low(str);
}
else
{
*strl=high(str);
}
printf("Converted string is %s.",*strl);
}
There is already a problem here:
So if you are saying this code is perfect and you want us to debug it and explain how (on earth) this works, then here you go.
In function int* low (char str), you have if(str[i]<91). Thats a problem right there. str is a char received as an argument, and hence str[i] is a straight compile-time error.
Another one to deal with is the return statement.
You have a statement:
return &str;
which would return the address of str, which by the way is a char, whereas function low is supposed to return a pointer to an int.
The same is applicable to high function as well.
Suggestion: Leave aside this bad code and get a beginner level C programming book first. Read it and the try some codes out of it.
A few inputs for improvement: (Which you may not comprehend)
change
void main()
to
int main(void)
Why? Refer this legendary post: What should main() return in C and C++?
Secondly, int both functions you are using strlen() in loop which will always return a fixed value. So, instead of
for (i=0; i<strlen(str);i++)
I'd suggest,
size_t strlength = strlen(str);
for (i=0; i < strlength; i++)
You can try the code and method as below:
#include <stdio.h>
#include <string.h>
char* caseConverter (char *str)
{
int i;
for (i=0; i<strlen(str);i++)
{
if(str[i]>=65 && str[i]<=90)
{
str[i]=str[i]+32; //To lower case
}
else if((str[i]>=97 && str[i]<=122))
{
str[i]=str[i]-32; //To upper case
}
else
printf("%c is not an alphabet \n",str[i]);
}
return str;
}
void main()
{
char inputStr[50]= "Stubborn";
char* opStr= caseConverter(inputStr);
printf("Converted string is %s",opStr);
}

C program. Call a function that prints a string

Is there a way to make a function that stores a string, and then call that function in int main() that displays it on the screen? I have been searching a lot and haven't found a clear example. Here is my code. I would like to be able to call it without using the if statement
#include <stdio.h>
/* function declaration */
int StrPrint(char *str);
/* main() function */
int main()
{
char str[] = "The string i am returning n";
int (*ptr)(char *str);
ptr = StrPrint;
if (!(*ptr)(str))
printf("Done!\n");
return 0;
}
/* function definition */
int StrPrint(char *str)
{
printf("%s\n", str);
return 0;
}
The code you've posted is far more complicated than the simple task you're trying to accomplish.
Why not something like this:
#include <stdio.h>
void StrPrint(char* str);
int main(void)
{
char str[] = "The string i am returning n";
StrPrint(str);
return 0;
}
void StrPrint(char* str)
{
printf("%s\n", str);
}
This conflicts slightly with your requirement, in that the function doesn't store a string it just prints out the string passed to it as an argument. But according to the code you posted, this looks like what you're trying to accomplish.
As mentioned by you if you don't like to use pointers it can be done as shown below:
#include <stdio.h>
int StrPrint(char s[])
{
printf("%s\n", s);
return 0;
}
int main()
{
char str[] = "The string i am returning n";
if (!StrPrint(str))
printf("Done!\n");
return 0;
}

Resources