Nope. Because none of the things you said seem to work for arrays either. Well, sizeof does, depending on the locality of the array, but that is also true for C strings.Originally Posted by Maz
Nope. Because none of the things you said seem to work for arrays either. Well, sizeof does, depending on the locality of the array, but that is also true for C strings.Originally Posted by Maz
prints the single char in array s, at index iAlso I would be thankful if difference between:
andCode:printf ("%c",s[i]);
prints the string (may be multiple chars), in array s, beginning at index i
andCode:printf ("%s",s[i]);
prints the string in array s, beginning at the first char in s
andCode:printf ("%s",s);
prints the single char in array s, at location s[0]. The name of the array will constantly have the address of the first element of it's array.
can be explained.Code:printf ("%c",s);
Alter ego, if you're still around, let us know where you are in your program, now.
Correct. assignment / comparison wont work for arrays either.
Mmm.. What do you mean by saying that sizeof() works for C strings?
there str is an array, but not string. (string cannot contain NULL byte other but at the end - bar does contain it). sizeof(bar) will result 20 on any system I own.Code:char str[20]="foobar";
there str2 is a C string (literal). It fullfills requirements for C string. How ever, sizeof(str2) will give the size of char * (pointer) type.Code:char *str2="foobar";
Hmm... Now you got me actually interested in what C standard does say. I found following:
ISO/IEC JTC1/SC22/WG14 N794
(correct me if I am looking for wrong document).
Now, out of the curiosity (yeah, I'm not a cat), why array assignment does not work?
If we assumed an array would specify a type, then:
Two types have compatible type if their types are the
same....
I even tested the compilation error gcc 4.3.2 gives:
if we have:
=>Code:char bar[25]; char baz[25]; bar=baz;
error: incompatible types in assignment?
(Well, I am by no means accusing gcc being wrong - I know it must be my ability to digest that text).
I would appreciate it if someone told me what do I miss here.
Obsession to networking and protocols made me cook up these:
NSN - Network Status Notifier
epb - Ethernet Packet Bombardier
T.H.O.N.G.S - Textmode Helper On Network Getting Sniffed
Nibbles - console UDP print listener/filter + something else
Feel free to try, comment and improve =)
Probably something like: sizeof("Maz"). Technically though, the operand is a (const) char[4].Originally Posted by Maz
Probably just a misleading error message, i.e., it is really trying to say that the types are incompatible for use in an assignment.Originally Posted by Maz
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks everyone. I was down with fever hence couldn't check the thread. Some engrossing discussions here . Big thanks for clarifying on types of data.
However I am still working on the program & have modified it a bit. My approach is to check for '/' & '*' if found true; fill the array with blanks & exit. Finally print everything that's up there in the array. However I am unable to pin-point my mistake because program is only accepting input (no output).
Code:#include <stdio.h> int main(void) { int c =0; int i =0; int s[1000]; int in_quotes = 0; for (i=0; i < 999 && (c=getchar())!=EOF; ++i) { s[i] = c; if (s[i] == '/' && s[i+1] == '*') /*entering the comment*/ { i = i+2; in_quotes = 1; s[i] = '/b'; /*filling blanks*/ if (s[i] == '*' && s[i+1] == '/') /* now exiting { i = i+2; in_quotes = 0; } } if (in_quotes == 1) { printf ("%s",s[i]); } } }
Last edited by alter.ego; 09-29-2011 at 01:56 AM.
Code:/* You're evaluating s[i+1] here... When will s[i+1] be set? */ if (s[i] == '/' && s[i+1] == '*') /*entering the comment*/
Obsession to networking and protocols made me cook up these:
NSN - Network Status Notifier
epb - Ethernet Packet Bombardier
T.H.O.N.G.S - Textmode Helper On Network Getting Sniffed
Nibbles - console UDP print listener/filter + something else
Feel free to try, comment and improve =)
The preferred way to work with a string of text is:
1) put the whole string (if not too huge), into a char array1
2) make all your tests and modifications that you need to do, on the array1 chars.
3) when finished with all your tests and changes, either compact array1, or copy the good parts you need, and put them into array2, or use sscanf() to take what you want from array1, and assign it to other variables. sscanf() is just like scanf(), but you need to first, add the name of the string that you want it to scan. Same format, otherwise.
In your program, you're assigning the char c into the array, before you make any tests, which seems awkward to work with, logically.
Glad you're feeling better, Alter ego.
Some posts in this thread were moved to Types in C.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks for helping me out; I am loving this.
In fact that is wrong & I figured that yesterday. It truly stands out for all wrong reasons. Glad you noticed that. I am still working on the code intermittently; will find a way to get it right out (now that you have given me some pointers).
Yes, I am feeling better, thanks for asking .
What are your logical rules for the program? Have you specified them?
e.g.:
1)
In any line of text, if two slashes ( // ) are next to each other, the rest of the line of text, is a comment.
2)
In any line of text, a slash and adjacent asterisk /* begin a comment. All text after that, until a */ is reached, is a comment.
and of course:
3)
All comments starting char, is overwritten with a \n (newline, and an end of string char), \0. Subsequent lines of comment text, including the newline and end of string char, are not copied into the new (non commented), output.
So, what's working, and what's not? < slacker >
Initially I am keeping it simple. I amonly checking comments between /* and */. If found true, count the caharcters between them & replace with blanks. That's my simple logic. Beats me , why it is not working?
Code:#include <stdio.h> int main(void) { int c =0; int i =0; int m =0; int s[1000]; int in_quotes = 0; for (i=0; i < 999 && (c=getchar())!= EOF; ++i) { s[i] = c; if (in_quotes == 0) { if (s[i] == '/' && s[i+1] == '*') { i = i+2; in_quotes = 1; } if (s[i] == '*' && s[i+1] == '/') { i =i+2; in_quotes = 0; } if (in_quotes == 1) { i++; for (m = 0;m <=i; m++) { s[m] = ' '; } } printf ("%s", s); } else { printf ("%c", s[i]); } } }
Last edited by alter.ego; 10-01-2011 at 12:37 AM.
@alter.ego ...
What I see is that you are very busy trying to code the solution to a problem you do not fully understand... Give yourself the problem... Sit thee down with no computer and two pieces of paper. On one page is a few lines of source code you have to decomment the other page is blank... according to the task you have to copy the source code to the second piece of paper but without the comments...
Go ahead, ACTUALLY do this... but make careful note of HOW you do it...
You might say, I copy the first word... Ok how do you copy a word? Well I copy each letter in turn...
But how do you know when to and when not to copy a letter?
Work the problem right down to super-idiot sized steps --because that's what you are dealing with, a super-idiot disguised as a computer-- and write a set of instructions detailing exactly how to copy that source code... Now you know how to solve the problem.
Next you need to spend just a few minutes of planning... based upon your instructions, you should decide what functions, variables, etc. you are likely to need and rough out the general structure of what you're going to write. For example: if there's something you have to do 2 or more times, it might be a candidate for a function. If you need to do something multiple times, in a row, you might consider a loop...
Ok now you have a plan... you can begin writing code. Work in small steps break it down into blobs... "user input", "display", "Calculation", "Files", etc get one section working before you move on to the next...
Then once it's all done... test your work until you are satisfied it works as intended.
4 steps... Analyse -> Plan -> Write -> Test ... the same for every program you write...
Study the problem - which is not as easy as it seems. We have years of language classes that, (perhaps despite the assertions of Mrs. Gibbs*), have actually become part of us.
So it's tough to somehow work on these simple language problems, as if we knew nothing about how language works. But that's what you have to do, to get the tiny steps you need, to tell the computer how to work through this exercise.
Do it by hand, until you can see these simple patterns. Then write them down in small steps.
That's the start of your program, in pseudo code. Now add to it, and when it's about right, THEN use it to code up the backbone of your program's algorithm.
Be careful where the newlines and end of string char's are. You can't see them, but they're a critical part of working with strings and chars.
*a pseudonym for your language teacher