Trentacle 112 Junior Poster in Training

My last post was uncharacteristically harsh. Sorry about that.

I don't have a good opinion of VB -- I feel that it's popular in spite of its design, not because of it. It suffers from an unfortunate family tree, and on top of that, it's not an open standard (big black mark in my book).

Granted, it doesn't really compete in the same areas with any of the languages I regularly use, so I don't have anything to say about its use in any particular application. I just wouldn't advise it as a general purpose language for beginners.

Trentacle 112 Junior Poster in Training

I may be mistaken but I believe you have to by [sic] QT

If the rest of your post is also 12 years out of date, I'm not sure why I bothered reading it.

Few would be developing VB.net apps with GUIs without using an environment that removes the grunt work of placing visual elements, or an interactive debugger that supports breakpoints and step-by-step source level debugging.

I fail to see how fewer people using VB is a bad thing.

Trentacle 112 Junior Poster in Training

When you declare an array (or any object) within a function, the memory allocated for it has automatic storage duration, meaning that once the name it was declared with goes out of scope, trying to access the memory (via e.g. a persistent pointer, like you have done) results in undefined behavior.

There are three ways to pass arrays around among functions without running into trouble.

  1. Declare the array in the calling code and pass a pointer to it to the called function, which merely manipulates its content as opposed to returning something with the return statement. This is probably the most popular method and is how standard library functions like fgets() work.
  2. Within the function, use malloc() to reserve space for the array in dynamic memory and return a pointer to the allocated memory. This has the disadvantage that you have to remember to free() the memory when you're done with it.
  3. Wrap the array in a struct. Arrays within structs are passed on the stack just like other kinds of objects (i.e., the array doesn't "decay" into a pointer to its first element). This has several disadvantages, notably that it's unnecessarily complicated for simply passing an array, and doesn't work for arrays of unknown size at compile time (except with C99's flexible array members).

So, yeah, pretty much those are your options.

Trentacle 112 Junior Poster in Training

Since you're not instantiating any objects of class Die, anything you want to use must be declared static. That includes all the variables that are defined directly within the class as well as the die() method.

That said, you don't have code showing how you expect to use the Die class (namely the body of main), so I could be way off base.

Trentacle 112 Junior Poster in Training

Why do you need an array that size?

How would you rewrite your program so that such a large array is not necessary?

Trentacle 112 Junior Poster in Training

Or

no strict 'refs';
$ARGV[0]->();

(This is not actually a recommendation, and I hereby disclaim any consequences of doing this in actual code, including but not limited to odd behavior, strange errors, problems debugging, write-only code, and enraged maintenance programmers.)

Trentacle 112 Junior Poster in Training

Well, yeah. &sub_name; just calls the sub_name subroutine. In your example you define four subroutines and call two of them. What would you expect to happen?

Trentacle 112 Junior Poster in Training

Nope. If you want it to do something when you run it from the command line, you need to put code in the top level of the file (not in a function).

You can easily make your script behave this way, though, if you add the following code to it:

if ($ARGV[0] eq 'thisismyfunction1') {
    &thisismyfunction1;
} elsif ($ARGV[0] eq 'thisismyfunction2') {
    &thisismyfunction2;
}

It's simplistic and won't scale well, but it works if it's used as you describe. Depends on what you need.

Trentacle 112 Junior Poster in Training

That's certainly one valid way to pass multiple values to a function, but it's not advisable. Your proposed solution is overly complicated, which makes it hard to document, hard to read, and hard to debug.

Furthermore, it seems in your case to be totally unnecessary. Since you're not doing anything with the array elements but printing them out, "pass-by-pointer"[1] would work just fine, and you wouldn't risk causing any side effects by simply passing a pointer to the original array. (If you want to guarantee that you don't change the original object within the function, declare the function parameter const -- that's what it's there for.)

If, on the other hand, you want to use a function that changes the array that's passed to it, without changing the original array, then your most viable option is to make a copy of the array and pass the copy instead of the original. This is not the same as passing an array "by value", but it will usually have the same effect. You can also use the struct trick described earlier to kind-of pass an array by value -- the disadvantage being that it won't work for dynamically sized arrays.

[1]: As opposed to "pass-by-reference", which exists in C++ but not in C. In C, arguments are always passed "by value", but some values happen to be pointers, as deceptikon correctly pointed out.

P. S. What book are you using? void main() and clrscr() are non-standard, and worrisome to see because they're …

Trentacle 112 Junior Poster in Training

You mean like

System.out.printf("%4.2f\t%4.2f\n", var1, var2);

Check out the Java API for PrintStream and Formatter to see details on printf.

Trentacle 112 Junior Poster in Training

Ok, wait.

If your first file says what position you want to change, why does your second file say what character you want to change? If the "reference" character in file 2 is different from the character found at the position given in file 1, do you still change it to the "mutant" (not "multant") form?

I might also ask, how do you do it in Excel?

Trentacle 112 Junior Poster in Training

atoi() has no way to recover from errors. Use strtol() as subith86 suggested.

Should you actually want to test whether a character is a digit, don't perform contortions; #include <ctype.h> and use isdigit instead.

Trentacle 112 Junior Poster in Training

I'm trying, but I can't figure out how your output follows from your input. Can you explain the problem more clearly?

Trentacle 112 Junior Poster in Training
WELCOME TO FORUMS, puppycrazy
WOULD YOU LIKE TO ANNOY YOUR USERS?(Y/N) : y
IN THAT CASE PLEASE CONTINUE USING ALL CAPS

WOULD YOU LIKE TO GET GOOD ANSWERS?(Y/N) : y
IN THAT CASE PLEASE CONSIDER PROOFREADING YOUR POSTS TO ENSURE THEY MAKE SENSE. ALSO,PLEASE!AVOID>USING~WEIRD.......><....PUNCTUATION, SPACES EXIST FOR A REASON
Trentacle 112 Junior Poster in Training

Generally speaking, anywhere in C you would see a call to malloc(), in Java (or C++) you'll find the word new instead. This also applies for strings, but strings are a little bit special in that there is a unique syntax for creating them (using "...") that doesn't use new. In either case, the result is the same: the standard library reserves the space to store the String when the constructor is called. Java doesn't have destructors (as such), so the space is only freed when the garbage collector gets around to it.

In C, I'd like to add, you do not need to declare the size of the char array to store a string when you initialize it with a string literal; since the size is known at compile time, the compiler can do it for you.

char str[] = "foo";
Trentacle 112 Junior Poster in Training

Have you noticed that n is only ever zero at the beginning of the program? I'm not 100% sure I understand the purpose of it, but I think you should at least reset it on each iteration of the while loop.

Furthermore, what happens when tmp ends up pointing to the head of the list? tmp isn't NULL, but tmp->prev is... that could be the cause of your segfault.

However, this is kind of convoluted for an insertion sort. Consider rewriting it.

Trentacle 112 Junior Poster in Training

I see what your problem is... but the advice I'm going to give is going to be more useful in the long run than telling you straight.

# after 'use strict'
use Data::Dumper;

# then in your code, when you want to see the contents of %seqs
warn Dumper(\%seqs);

Put the warn in your first while loop so you can see the hash being built.

Dumper is something you should know about, it's invaluable for debugging stuff like this.

Trentacle 112 Junior Poster in Training

It's only a practical configuration of your linux system. It doesn't prevent you from adding a shebang line if you want. I've been using this trick for some time now and it is very nice. Why not use all the tools that come with a linux system ?

Well, I could pretty easily install software and graphics to make my desktop look just like Windows 7, and with some dedication I could rename all the binaries to end in .exe, and then there's some trickery that I could use to make all my paths appear to start with C: and use backslashes instead of forward slashes, if I really wanted my Linux system to act like Windows... but I have no idea why I'd want that. Similarly, I don't want my Linux system to assume filetypes based on extensions, because that's one of the things that often frustrates me in Windows. But no, your way is good, too. ;)

Trentacle 112 Junior Poster in Training

@valorien: also read this as an alternative to the shebang line: http://www.daniweb.com/software-development/python/code/241988

This sounds like a bad idea. Non-portable, and it means all your Python scripts have to end in .py (and if by chance you were to have an actual binary end in .py you wouldn't be able to use it). Is there some problem with using a shebang line that this approach is trying to solve?

Trentacle 112 Junior Poster in Training

userRequest is local to getCommand, which means its memory disappears when control returns to main(). strtok() doesn't allocate new memory -- it just zero-terminates a token in the source string and returns a pointer to its first character. Therefore all the addresses in the parameters array are invalid when getCommand returns.

I would do something more like size_t getCommand(char *dest[], size_t size, char *source); so you can allocate the necessary memory in the caller and pass in a pointer to it. Implementation left as an exercise for the reader.

Trentacle 112 Junior Poster in Training

const-qualified variables aren't the same as constants.

int const i = 5;

5 is a constant. i is a variable that happens to be const-qualified, which means you can't modify it directly. Constants can never be modified, because at runtime they usually don't exist. const-qualified variables, well, it depends.

const is mostly a way of documenting variables that you intend not to modify. Consider

size_t strlen(const char *s);

The word const here indicates that the function strlen will not try to modify the referent of s (here an array of char).

An important thing to realize about const is that it's applied to variables, not objects. Observe:

int i = 0;
int *p = &i;
int const *q = &i;

Although p and q both refer to the same object, only p can safely be used to modify it. Any attempt to modify *q, which is const, causes undefined behavior (I think -- don't have my C99 at hand).

Furthermore, const-ness is not part of the value of an expression, but merely a property applied (at compile time) to certain names. The purpose of const is to allow the programmer to state his intent not to modify a certain variable within a certain scope, and for the compiler to assume that variable is not being modified, so that optimization can take place.

Your question about linkage is rather puzzling, because linkage is determined by the scope of the declaration and has nothing to …

Trentacle 112 Junior Poster in Training
mt = (mytype*)calloc(1, sizeof(mytype));

Better is

mt = calloc(1, sizeof *mt);

1) don't cast the result of malloc() (and friends), because all it does is hide a useful warning; and
2) sizeof *mt is shorter than sizeof(mytype) , and you don't have to change it if you later change the type of mt.

Other than that, yep, pretty much, just what gerard4143 said.

I have a personal vendetta against typedef, so I would eliminate that, too.

Trentacle 112 Junior Poster in Training

>void main() is not legal C (under a hosted environment).
Incorrect. Since you seem to be a standard reader, look it up and you'll see "or in some other implementation-defined manner". This is an escape clause that allows implementations to support extended functionality, such as not requiring a return value (ie. void main) or additional parameters such as the common envp third parameter.

Conceded. I should have said "not standard" rather than "not legal", as the standard allows, but does not dictate, this form.

Your three parameter version falls under the escape clause, though it is mentioned as a common extension in annex J section 5 of C99.

My careless mistake. In fact, I was trying to say (correctly) that the three parameter version was nonstandard, when I discovered it in the actual standard. I revised my post without paying attention to the context of its mention. Thank you for the correction.

Trentacle 112 Junior Poster in Training

If the main() takes 3 arguments i.e. int argc,char * argv[],char *env[] and SINCE C DOES NOT SUPPORT FUNCTION OVERLOADING ,y does the c compiler does not give error for simply

void main() //that is no arguments at all

OR

void main(int argc,char *argv[]) //2 arguments

There are two things wrong with your question. First, main() returns int. Always and for ever. void main() is not legal C (under a hosted environment). Your options are (under C99):

int main(void);
int main(int argc, char *argv[]);
int main(int argc, char *argv[], char *envp[]);

Also, int main() isn't a prototype, so it can refer to main no matter what parameters it takes. void main() does not imply no parameters at all, but merely that the number and type of parameters are not known.

As for the actual question, the answer is that there's no overloading going on for the simple reason that there's no conflict. A C program can have at most one definition of main(). The compiler will see which version you used and generate the appropriate code to initialize whatever parameters your program expects. This isn't overloading or anything like it.

Trentacle 112 Junior Poster in Training

So fopen just hangs, without returning? Interesting.

The usual suspects for not being able to open a file (e.g., wrong case, bad permissions) may not apply in your situation -- but I'm not entirely certain what your situation is, and I don't think I can be any more help at this point. Perhaps somebody familiar with cygwin will come along to supply advice.

Trentacle 112 Junior Poster in Training

Sorry, missed the second page. Everything I had to say has been said

Trentacle 112 Junior Poster in Training

If you're using Cygwin, it's probably best to say it upfront -- avoids confusion.

For clarification, can you tell us whether fopen returns NULL when you open the file in a subdirectory?

Trentacle 112 Junior Poster in Training

Second what WaltP says, plus don't use scanf for interactive user input, check the return values of standard library functions, and use fflush(stdout) after printing a prompt that doesn't end with '\n' to make sure it gets printed.

1 is not a portable value to pass to exit(), and returning non-zero usually means that some error occurred. Selecting the "exit" function from the menu shouldn't result in an error.

Finally, I can't imagine what version of "shutdown" you're using if -s means "halt", -l means "log off", and -h means "hibernate".

Trentacle 112 Junior Poster in Training

It's not too difficult.

count is just a distraction.

b starts at 10 and increments until ugly_string is 0. So the useful part of the string is from ugly_string[31] to the end; the first 31 chars are padding just to obfuscate it.

a is set to the next character in the string for each iteration of the external loop, and decremented down to 64 -- so the inner for loop just runs N = ugly_string - 64 times.

c starts at 10 and increments until it reaches 90, but it's only used when its value is 90 and subsequently knocked back down to 10.

Interesting notes are the repetition and similar groups of 4 characters in the source string. There are, umm, 46 lines of output and 42 groups of four, plus 3 extra characters -- note that there is no terminating newline on the output, and I think we definitely have a pattern there.

Come to think of it, it makes sense that each character in the source string tells you how many times to print the next character -- the last part of the conditional tells you whether to print ' ' or '!' -- the middle part of the conditional tells you when to print a newline instead -- yeah, without working out the details, that's your program explained in a nutshell.

While it's not by far the best obfuscation I've ever seen, it's certainly a valiant effort.

Trentacle 112 Junior Poster in Training

yes but this function is not found in # include <stdio.h>

Erm, yes, it is, at least in C.

is it impossible to directly convert char to int ?

No, it's perfectly possible. You did it yourself, in your first post. But the way you phrased the question made it sound as if what you really wanted was to read an integer value from the contents of a char *. That's what the strtoX functions are for.

Perhaps you could clarify what you mean by "get integer value from the console" and "convert char to int", because I think both those questions have been adequately answered already.

Trentacle 112 Junior Poster in Training

Sorry, my bad!

Well, since you're repentant, I won't call the Standards Police... ;)

C++ mixed in with C is one of my pet peeves. Sorry for sounding snippy.

Trentacle 112 Junior Poster in Training

Also, you don't need

struct stacktype s1;

on line 62, you can just use stacktype s1 .

Only in C++. Please don't do that in C.

Trentacle 112 Junior Poster in Training

Interactively, use fgets() to get a line, then strtol() to convert it to a long.

If you're reading from structured input, use scanf().

@alexchen: C is not the same as C++. Please take that cin nonsense somewhere else.

vedro-compota commented: +++++ +1
Trentacle 112 Junior Poster in Training

Well, it's not really a two-dimensional array -- it's a pointer to (the first element of an array of) pointer to (the first element of an array of) short.

The comp.lang.c FAQ, section 6, might help you understand, especially question 6.16.

Trentacle 112 Junior Poster in Training

dear all

i am new to perl. when i run my basic perl program as follows

print<<EOF;

HELLO MY NAME IS MANISH ARORA
EOF

i am getting this error

Can't find string terminator "EOF" anywhere before EOF at PERL.plx line 1.

please help

Sounds like there's a stray space after the EOF. Careful with that.

(BTW, there's no need to yell. Perl understands lowercase letters quite as well as caps.)