Expected expression before 'char' [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm having a pretty hard time working on this assignment I have for one of my CSC classes. I am making a character array from my name (ie John Doe). I need to pass a pointer of my character array into a function that outputs the number of characters stored in your character array. Then I need the function to also return this integer back to my main function.
If anyone would like to help me in advanced
I need to spell it completely backwards. i.e. (eoD nhoJ)
I need to inform the user of which element of my array the space in there name is. I also need to return this value as well.
Then last but not least I need to reverse the name i.e. (Doe John)
Please help me with this. I've only posted the part of the assignment I've actually managed to figure out. Thank you all in advance.
#include <stdio.h>
#include <string.h>
int main ()
{
char name [25]={'\0'};
fgets(name,sizeof(name),stdin);
printf("You Entered: %s \n", name);
printf("There are %u characters. \n", strlen(char * name));
}

strlen(char * name) ==> strlen(name)
You should not use char * before string while calculating length
EDIT
If your intention is to write your own function for some practice.
declare function
unsigned int my_strlen(char *); //argument needed is character pointer
And define
unsigned int my_strlen(char *str)
{
//find length..
return length;
}
Function call
printf("There are %u characters. \n", my_strlen(name)); // passing starting address of character array to a function.
Here also you should not use char * before name.
All your questions can easily found on Google. just doing simple search you can found.
If you want to do them by your self. you need to read c tutorials.

Change:
printf("There are %u characters. \n", strlen(char * name));
by:
printf("There are %u characters. \n", strlen(name));

Related

Program crashes in the `scanf` function. Why? [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 5 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
char key, vector;
char plainText[101];
char cipher;
int i, cipherValue;
int keyLength, IVLength;
scanf("%s", key);
scanf("%s", vector);
return 0;
}
My program crashes after I input values for the scanf parts. I don't understand why.
The problem with
scanf("%s", key);
scanf("%s", vector);
is:
key and vector are of type char, not pointers to char. The can hold one character only.1
With %s scanf expects a pointer to char. As it stands right now, you
are passing uninitialized integer values as if it were pointers, that's
undefined behaviour and your program crashes as a result of it. The compiler
must have given you a warning about this, don't ignore the compiler's warnings,
they are there to help you, not annoy you.
The correct version:
char key[101], vector[101];
...
scanf("%s", key);
scanf("%s", vector);
// or to limit the number of bytes
// written in the buffer, as pointed out
// in the comments by user viraptor
scanf("%100s", key);
scanf("%100s", vector);
For more information about scanf, please read the documentation
Footnote
1A string in C is a sequence of characters that ends with the
'\0'-terminating byte. A string with one single character needs a char array
of dimension 2 or more. In general, a char array of dimension n can store
strings with maximal length of n-1. You have to keep that in mind when passing
pointers to char to functions when they expect strings. A pointer to a single
char will lead to undefined behaviour because it overflows.

Storing integer in char buffer array becomes binary value [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
I have an array of integers, for example:
I memcpy the value into a char buffer, and the next time I retrieve it, it becomes a binary value, for example, 00000 -> 16. How do I avoid this?
Here is a snippet of my code:
char buf[BUFSIZE];
int outgoingPorts[4] = { 100000, 100001, 100002, 100003 };
memcpy(buf, &outgoingPorts[0], sizeof(outgoingPorts[0]);
printf("Port no: %i\n", buf);
Here, buf or the first outgoing port is 16 instead of 10000.
First of all, you're using wrong format specifier altogether, for the content of a char pointer to be printed, you need to use %s, provided that char array in null-terminated. Otherwise, you'll face undefined behaviour.
Then, if you really want to store an int value in a char array, the most suited way to go is to make use of snprintf() to print that value in the char array. Maybe the following pseudo-code will help you
char carr[16];
snprintf(carr, 16, "%d", outgoingPorts[0]);
printf("%s\n", carr); //should print 100000
FWIW, using wrong or mismatched format specifier invokes undefined behaviour.

invalid conversion from `char' to `char*' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
code is :
int main() {
int i,last;
char TXT[500];
printf("Donnez un exemple ?\n");
gets(TXT);
last = strlen(TXT);
for(i=0;i<50;i++){
if (i==0){
strcpy(TXT[1],TXT[0]);
} else {
strcpy(TXT[i-1],TXT[i]);
}
}
getch();
return 0;
}
error in line : strcpy(TXT[1],TXT[0]);
What is the cause of the problem ?
The strcpy function takes two char* (technically, a char* and a const char*). Moreover, it is not allowed to pass strcpy overlapping buffers.
It looks like you wanted to write
TXT[i-1] = TXT[i];
(this would delete the leading character from the string).
for(i=1 /* one, not zero */ ; i<50 ; i++) {
TXT[i-1] = TXT[i];
}
Good, but I want to use strcpy. How?
You are not allowed to use strcpy without an intermediate buffer. If you must use strcpy, do it like this:
char TXT[500], TMP[500];
printf("Donnez un exemple ?\n");
fgets(TXT, 499, stdin);
strcpy(TMP, &TXT[1]); // Note that 'for' loop is no longer required
TXT is an array of 500 characters.
So, TXT[1] and TXT[0] are individual characters (just one single letter).
TXT[0] is the very first character in the array.
TXT[1] is the second character in the array.
The function strcpy expects you to pass POINTER-to-characters (type char*) for both parameters.
And instead, you're passing a single character.
Can you explain what the purpose of this program is?
Maybe we can help you fix it then.
TXT[i] is of type char. But strcpy expects parameters of type char* since it operates on null-terminated strings. Hence the compilation error.
As for how to fix it, that depends on what your code is trying to do. Perhaps all you meant to do was
TXT[1] = TXT[0];
STRING FUNCTIONS WORKS ON STRING NOT ON CHARACTERS
ARE YOU TRYING TO SWAP THE WORDS OR CHARACTERS ?

How to print a huge string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm a beginner and I need to know how to print an entire help page in C.
I am trying:
unsigned short * entireHelpPage;
unsigned int * someString:
printf("comparing %s to %s", someString, entireHelpPage);
this is printing something like this :
comparing Dog to Dog is a domestic animal.. blah blah.. Dogs are bred in mos
As you will see that entireHelpPage is not getting displayed completely when I try to print it.
Please let me know how to get it to print the entire help page.
Use a loop to get around limitation in printf() or potential memory/display problem.
OP is experiencing some issue. printf() should be able to print at least 4095 characters before it has troubles. To get around a non-conforming issue, use a loop. To find unexpected non-printable characters, print them out in a special fashion.
const char *s = (const char *) entireHelpPage;
fputs(">", stdout);
while (*s) {
if (isgraph(*s)) {
fputc(*s, stdout);
}
else {
fprintf(stdout, "[%02X]", (unsigned) *s);
}
s++;
}
fputs("<\n", stdout);
Further: it is strange to use unsigned short * as a pointer to char* data. I suspect that the tailing memory pointer to by entireHelpPage is being unexpectedly over-written by code. It could be that entireHelpPage is a buffer about 400 bytes and is simple not larger enough for the help page.

I am trying to do a program that picks a vowel from the user input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *v= "a";
char *o='e';
char * w='i';
char *e='o';
char *l='u';
char *u[1];
printf ("please enter your character\n");
scanf ("%c",& u);
if (u == v){
puts("the character is it a vowel\n");
}
if (u == o) {
puts("the character is it a vowel\n");
}
else
puts("the character is a constant\n");
system("PAUSE");
return 0;
}
i need help in getting the right answer in finding a vowel from the user input.
First of all, shame on you for ignoring all of the compiler warnings you certainly received. They are there to help prevent you from doing "stupid things."
And why all this nonsense? This is the first of the "stupid things" the compiler is trying to tell you about.
char *v= "a";
char *o='e'; // invalid - Initializing a pointer to a constant 'e' (101).
char * w='i'; // invalid
char *e='o'; // invalid
char *l='u'; // invalid
Are you familiar with how pointers work? If not, I suggest you do some reading and understand them.
The first line makes sense - you're making a string and pointing char* v to that string.
But there's really no point in using pointer for those characters - or even variables at all. Just compare them directly:
char my_character;
if (my_character == 'a') {
// do something
}
And as for reading the character, again, you're using pointers when it doesn't make sense:
char *u[1]; // why?
Instead, just define a single char variable. Now, go look at the documentation for scanf. Giving it a format string of "%c" means, "I want to read just one character". Then, you need to tell where scanf to put it. You do this by passing it the "address of" the variable you want to store it in. You do this with (unsurprisingly!) the address of operator &.
char input_character;
scanf("%c", &input_character);
Armed with this information, you should be able to complete your work. Next, I suggest you look into the switch statement.
Finally, you must use consistent formatting (indentation, spacing) and use meaningful variable names, if you have any desire of ever being taken seriously as a programmer. Spelling out "vowel" for your pointless variables may be "cute" but it's total nonsense.
Most importantly, you should never write a single line of code, unless you understand exactly what it does. If you do this, then do not go asking anyone for help (especially not StackOverflow). If you can't explain what your code is doing (or at least what you think it's supposed to do), then you don't deserve for your program to work.

Resources