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.
Related
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 ?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
i'm making a program in C. that keeps track of items in a warehouse.
and i want to force the user to include at least one number!
example dvd1, dvd2. hello1, hello20
is there a way to do this?
at this moment i am using scanf.
and i want the product code to have the requirement format of xx-xxx-xxx were x are numbers.
i'm using scanf ( %[0-9-]s
Mvh Anton!
scanf doesn't work like that, it doesn't have in-depth validation.
You need to read the input into a char array, then loop through each character and see if it is a digit.
Something like this (untested):
char buffer[1000];
int i = 0, hasDigit = 0;
scanf("%s", buffer);
while (i < sizeof(buffer) && buffer[i] != 0 && !hasDigit)
{
hasDigit = isdigit(buffer[i]);
i++;
}
// if hasDigit is 0, there are no digits
Note: scanf isn't great, since if you enter more characters than fit in the buffer it can cause a buffer overflow. It is better to use fgets(buffer, sizeof(buffer), stdin);
Read the input and you can iterate through as in This SO question. You can check to see if chars match the input you want pretty easily from that point on.
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));
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.
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 have written the following code and it works fine. But before I made changes i had entered a few statements which I expected would work but didn't. Just as a trial, i made changes and it worked. Please clarify what I was doing wrong. I am trying simple programs initially to make my understanding of Pointers better.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int i;
char *instring,*outstring;
char ch,p;
instring = (char*)malloc(15*sizeof(char));
outstring= (char*)malloc(15*sizeof(char));
printf("Enter the string:");
gets(instring);
printf("Enter the character to be removed:");
scanf("%c",&ch);
for(i=0;i<strlen(instring);i++)
{
if( *(instring+i) != ch)
{
*(outstring+i) = *(instring+i);
printf("%c",*(outstring+i));
}
}
Under if statement, I had written the following:
if(*(instring) != ch)
{
*outstring = *instring;
printf("%c",*(outstring));
instring++;
outstring++;
}
Why doesn't this work?
I'm not sure what you mean by not working, but if your non-working function looked like this:
for(i=0;i<strlen(instring);i++)
{
if(*(instring) != ch)
{
*outstring = *instring;
printf("%c",*(outstring));
instring++;
outstring++;
}
}
Then it looks like the problem is that you are only incrementing instring if it's current character doesn't match ch. So if instring's current character does match ch, you will loop forever and the process will hang.
Also, if all you want to do is print out the string without the character to be removed, you don't need outstring. Just do e.g.
printf("%c",*(instring));
On the other hand if you also want to store the string in outstring with the character removed, you can't increment the pointers in tandem like you are doing. Because when you hit the character to be removed, you want to increment the instring pointer to move past it, but not increment the outstring pointer since you haven't added anything to that string.
It does work. But you loose both strings (pointers to them)
Try to store pointers to the beginning of both strings in another two pointers (char*) and then do your cycle.
And at the end, print the pointers you stored before - because they still point to your strings. When you increment pointers instring and outstring, they don't point to your strings anymore - they point to the very end of these strings.
Play with it a little bit more and you'll see :)
edit: Well, no, I've been writing faster then reading, sorry. Your (another) problem is even sooner - comparison. You are comparing instring with character and you change it only if condition is true. So you are comparing the same thing over and over and over.
It's tough to tell, what your problem really is.