Characters not showing up [C] [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm making a fun little text-based game for fun and for some reason some text aren't showing up for the username.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
/* Forward declarations -- Prototypes */
void askCharacterName(char *name);
void printMainMessage(char* name);
int main(char* username) {
askCharacterName(username);
char* temp;
temp = &username;
printMainMessage(temp);
return (0);
}
void askCharacterName(char *name) {
char username[20];
printf("What is your desired username?");
scanf("%s", &username);
return *username;
}
void printMainMessage(char *name) {
printf("Hello %s. Welcome to Lamescape!\n", name);
}
Here is my output:
Welcome []. Welcome to Lamescape!

A couple of things:
Your main function should have a different signature.
Consider allocating your memory at a higher level so it is available in lower
levels of the program.
When modifying buffers in c, always pass their size too.
Functions with void return values are not expected to return anything.
After fixing these problems your errors went away.
#include <stdio.h>
#include <stdlib.h>
/* Forward declarations -- Prototypes */
void askCharacterName(char *name, unsigned size);
void printMainMessage(char* name, unsigned size);
int main()
{
char namebuffer[100];
askCharacterName(namebuffer, 100);
printMainMessage(namebuffer, 100);
return 0;
}
void askCharacterName(char *name, unsigned size)
{
printf("What is your desired username?");
scanf("%s", name);
}
void printMainMessage(char *name, unsigned size)
{
printf("Hello %s. Welcome to Lamescape!\n", name);
}
Passing the size has no immediate effect here. I leave it up to you to figure out how to ensure that the buffer is never used beyond its bounds.

The username in askCharacterName has its memory content at the program stack.
void askCharacterName(char *name) {
char username[20];
printf("What is your desired username?");
scanf("%s", &username);
return *username;
}
Allocating the space in heap for username in main looks to be what you are looking for.

Related

how to swap two character of a character array? in c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How can you replace two characters in a character array? For example:
charecter array : peter
Replace the two characters p and t and give the following output:
teper
its my try(it is wrong):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(char*,char*);
main() {
int n,i,j;
char str[30][30];
printf("how many names?:");
scanf("%d",&n);
for(i=0;i<n;i++) {
printf("name %d?",i+1);
scanf("%s",str[i]);
}
for(i=0;i<n;i++) {
char ch1,ch2;
printf(" which letters of name %d?:",i+1);
scanf("%c%c",&ch1,&ch2);
swap(&ch1,&ch2);
printf("\n %s",str[i]);
}
}
void swap(char *a,char *b){
char temp;
temp=*a;
*a=*b;
*b=temp;
}
You can do something like this
// you can use the same logic in c++
#include <stdio.h>
void swap(char * a, char * b) {
char temp = *a;
*a = *b;
*b = temp;
}
int main() {
char str[] = "peter";
printf("before -> %s\n", str);
swap(&str[0], &str[2]); // swap p and t
printf("After -> %s\n", str);
}

How can I convert char[] array to char* [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to convert my char[] array to char* string in C programing language. Here is what I did. But I can't reach the solution . Please help me. Here is my code:
#include <stdio.h>
void fnk(char *chr){
printf("%s",chr);
}
int main(){
char charfoo[100];
int i=50;
gets(charfoo);
for(int j=0;j<i;j++){
fnk((char*) charfoo[j]);
}
}
I think you mean the following
fnk( &charfoo[i]);
Take into account that it would be better to write the loop the following way
for( int j=0; charfoo[j]; j++ ) {
fnk( &charfoo[j]);
}
Also function gets is unsafe and is not supported any more by the C Standard. Instead use function fgets For example
fgets( charfoo, sizeof( charfoo ), stdin );
In this case the loop can look like
for( int j=0; charfoo[j] != '\0' && charfoo[j] != '\n'; j++ ) {
fnk( &charfoo[j]);
}
If you want to output just one character in the function then the function should be defined like
void fnk(char chr){
printf("%c",chr);
}
and called like
fnk( charfoo[j]);
If we switch from gets() to fgets() as Vlad from Moscow suggests, then the return value from fgets() is of the same type as the argument to fnk() so we can just pass the result along:
#include <stdio.h>
void fnk(char *string) {
printf("%s", string);
}
int main() {
char charfoo[100];
fnk(fgets(charfoo, sizeof(charfoo), stdin));
return 0;
}
The next step in sophistication would be to save the result of fgets() to a char * pointer and test if it's NULL or not before passing it along (or not) to fnk().
If you want an actual char * copy of the array charfoo[] to exist in memory and not simply pass off char[] arrays as char * pointers, you could do the following which throws in lots of error checking too:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fnk(char *string) {
printf("%s", string);
}
int main() {
char charfoo[100];
char *input = fgets(charfoo, sizeof(charfoo), stdin);
if (input != NULL) {
char *string = strdup(input);
if (string != NULL) {
fnk(string);
free(string);
}
}
return 0;
}
But in all cases, it seems like you're making things more difficult than they need to be.
Calling fnk with i would just be the same call 50 time -- assuming you want to use j instead the code would look like this
#include <stdio.h>
void fnk(char *chr){
printf("%s",chr);
}
int main(){
char charfoo[100];
int i=50;
gets(charfoo);
for(int j=0;j<i;j++){
fnk(charfoo + j);
}
}
be aware that gets could cause buffer overflow if the input is longer than the charfoo (i.e. 99 bytes + newline + null) -- but that is a different story

How to toupper char array in c? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
whats wrong here?
I want to check if the char in char array islower, so if it is it should be changed in uppercase.
#include <stdio.h>
int main(int argc, char *argv[]) {
char arr[100];
scanf("%s",&arr);
for(int i=0;i<sizeof(arr);i++){
if(int islower(arr[i])){
arr[i] = toupper(arr[i]);
}
}
printf("%s",arr);
return 0;
}
To measure the length of a string properly, use strlen, not sizeof
for(int i=0;i<strlen(arr);i++){ // Don't use sizeof on this line
Here's a simpler version:
#include <stdio.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
char arr[100];
scanf("%s", arr);
for(int i=0;i<strlen(arr);i++){
arr[i] = toupper(arr[i]);
}
printf("%s",arr);
return 0;
}
Or even:
#include <stdio.h>
#include <ctype.h>
int main(void) {
char arr[100];
scanf("%s", arr);
for(char* c=arr; *c=toupper(*c); ++c) ;
printf("%s",arr);
return 0;
}
You are missing an include #include <ctype.h>
Also you don't need your if statement. toupper takes care of that internally (if you really want to keep islower remove the int in your if statement).
Add the header that declares islower and toupper.
#include <ctype.h>
In addiition,
if(int islower(arr[i])){
is not right. Remove the int.
if(islower(arr[i])){
whats wrong here?
The line: if(int islower(arr[i])){ fails compile for bad expression.
Change to: if(islower(arr[i])){
And in this line in your code may be looking beyond where it should:
for(int i=0;i<sizeof(arr);i++){
as you may be looking at space past the string terminator:
|s|t|r|i|n|g|\0|<unknown contents here, part of your legal memory, but are not part of the string>
it should be:
int len = strlen(arr);
for(int i=0;i<len;i++){

new programmer here,whats wrong with this stack program? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
when i execute it the function do not work why?
#include<stdio.h>
struct stack{
int x[10];
int last;
};
void init(struct stack *s)
{
s->last=0;
}
void insert(struct stack *s)
{
int a;
while(a!=0)
{
int i;
printf("Enter the value\n");
scanf("%d",&i);
s->last++;
s->x[s->last]=i;
printf("%d",s->x[s->last]);
printf("enter 1 to continue 0 to exit\n");
scanf("%d",&a);
}
}
int main()
{
struct stack s;
int y,z;
printf("Trying out stacks\n");
printf("\n______________\n");
init(s);
insert(s);
return 0;
}
In function insert(), you declared
int a;
and then without initializing a you are doing the following,
while(a!=0)
will give Undefined Behaviour.
The following lines can leads buffer overflow,
s->last++;
s->x[s->last]=i; // no restriction applied on last
last can be more than 9 which can cause buffer overflow as x[10].

How to cast a void pointer to point more than one struct? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Having Problems about how to access other structure membera with void pointer of other function??
typedef struct
{
char Buf[20];
char Str[20];
}Sample;
typedef struct
{
char Data[20];
int i;
} Test;
Void pointer structure
typedef struct
{
void *New;
int j;
} Datastruct;
int main()
{
//i am confused with first line
Datastruct->New = &Sample;
strcpy((( sample*)Datastruct->New )->Buf,"adam");
printf(" Datastruct->New->Buf");
Datastruct->New = &Test;
strcpy((( Test*)Datastruct->New)->Data,"Eve");
printf("Datastruct->New->Data");
return 0;
}
please let me know how to access members of other structures via void pointers
The compiler is also confused about first line; you can't take the address of a type. As for following void pointers, you've got the right idea: cast it to the type of pointer you wish to treat it as.
Here is a fixed version which actually compiles and works without errors:
#include <string.h>
#include <stdio.h>
typedef struct {
char Buf[20];
char Str[20];
} Sample;
typedef struct {
char Data[20];
int i;
} Test;
typedef struct {
void *New;
int j;
} Datastruct;
int main() {
Datastruct d;
Sample s;
d.New = &s;
strcpy(((Sample*)d.New )->Buf,"adam");
printf("Datastruct->New->Buf\n");
Test t;
d.New = &t;
strcpy(((Test*)d.New)->Data,"Eve");
printf("Datastruct->New->Data\n");
return 0;
}
In your original you were confusing -> with . and types (e.g. Datastruct) with variables of that type.

Resources