deceptikon 1,790 Code Sniper Team Colleague Featured Poster

In the book called gnu c manual I read that the following code has an undefined behavior i = ++i + 1; Because since the variable i is modified more than once between the sequence point,it is not sure that what value will 'i' have.

That's correct.

But it is evident that the final value in 'i'(if i=1) would be 3.

It's not evident at all. Just because you can't think of a way the result could be anything other than 3 doesn't mean there isn't a way, or that a compiler won't handle things differently than you expect. In the case of the increment operator, it's only guaranteed that the write happens before the next sequence point. If that write is delayed until after the addition of 1 to i, you'll get a result of 2:

; Conforming assembly code for i = ++i + 1
mov r1, i
inc r1
mov r2, 1
add r1, r2
mov i, r2
mov i, r1 ; Oops, the write for ++ was not where you expected

This is quite different from the evaluation you were expecting that produces a result of 3:

; Conforming assembly code for i = ++i + 1
mov r1, i
inc r1
mov i, r1 ; The write for ++ is were you expected
mov r2, 1
add r1, r2
mov i, r2

Both are perfectly valid ways a compiler might generate machine code according to the rules …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I think your problem lies within understanding recursion

Or just don't use recursion. Honestly, I think that's the better choice for a linked list because the risk of stack overflow is greater when the recursive growth is linear or worse. Also, the iterative form of linked list appending isn't more complex:

node *addNode(node *list, const char data[])
{
    node *temp = malloc(sizeof *temp);

    if (temp) {
        temp->next = NULL;
        temp->data[0] = '\0';
        strncat(temp->data, data, DATA_SIZE);

        if (!list)
            list = temp; // Special case for an empty list
        else {
            struct node *it = list;

            // Find the end of the list
            while (it->next)
                it = it->next;

            it->next = temp;
        }
    }

    return list;
}

There are a couple of things to note over the recursive implementation that aren't directly related to recursion:

  • A direct translation would result in a potential memory leak if malloc() fails, so instead I used a second pointer to hold the new node until a location is determined. If malloc() fails, no changes are made to the list.

  • strncpy() is often misunderstood, and that can result in inefficient code. For strncpy(dst, src, count), count characters are always copied into dst. If there aren't enough characters in src, the remaining spots will be filled with '\0'. If src tends toward being much shorter than count then strncpy() could spend a prohibitive amount of time copying useless null characters.

    I used strncat() instead, because its behavior is more consistent with what people expect strncpy() …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster
Begginnerdev commented: Love LMGTFY +6
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

is this code will run on all compiler

All C99-compatible or C++ compilers, sure (due to the // style comments). ;) I'd also nitpick your prompts that don't end in a newline because they might not show up on all systems before scanf() blocks for input. But those are minor issues for strict conformance.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I try to understand any person I'm communicating with, figure out their native language and country of origin, level of education, perhaps even religious and political beliefs.

Only then can I offend them as efficiently as possible. :D

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

getch() don't need press <Enter> every time after user enter any character
and getch() don't print character which user enter it

That doesn't help if getch() isn't supported by the compiler. It's fine in your world of Turbo C, but your code will fail to compile almost everywhere else.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

if(ch==69||ch==101)
else if(ch==81||ch==113)

What do 69 and 101 mean? What do 81 and 113 mean? If you force me to memorize ASCII/Unicode or go to asciitable.com to figure out your code then your code is bad. Use the character literals that correspond to what you want, it makes the code much clearer:

if (ch == 'E' || ch == 'e') {
    ...
}
else if (ch == 'Q' || ch == 'q') {
    ...
}

You can also use toupper() or tolower() to make a single test:

if (toupper(ch) == 'E') {
    ...
}
else if (toupper(ch) == 'Q') {
    ...
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You can use free SMTP servers such as those with Gmail.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

...seriously? I mean, come on, did you really think "Help?" would be productive in the slightest? Ask a real question.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I haven't tried windows 8 yet,I can't find a site to download,thanks

http://windows.microsoft.com/en-US/windows/buy

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Seems simple enough:

#include <iostream>

class M_48 {
    static int instances;
public:
    M_48() { std::cout << ++instances << ": Foo!\n"; }
};

int M_48::instances = 0;

// This is the part you really want
int main()
{
    M_48 foo[37];
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

it mimics the function system from Windows

system() is a standard C/C++ function, it's available on Linux too. ;) You probably knew that, of course, but your choice of phrase could be confusing to less experienced folks.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

sizefn_t is a typedef for a pointer to a function. The typedef simplifies the syntax for said pointers because otherwise the syntax is pretty awkward:

// Declare a pointer to function and point it to useSizeFace()
double (*sizefn)(double*) = userSizeFace;

All the typedef does is wrap the awkwardness into a nice little package that you can then use directly just like any type:

// Hide the function pointer syntax behind a typedef
typedef double (*sizefn_t)(double*);

// Exactly the same as the first declaration for sizefn
sizefn_t sizefn = userSizeFace;
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Not to mention it wouldn't pass code review in any development house I've worked with (assuming it compiled). The code itself is brittle and the solution is clearly written by someone who hasn't done much date and time programming.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Holes in your work history are more damning than history in unrelated fields. So yes, add that job.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I have found the solution in other websites that we have to clear the buffer and use cin.ingnore(). Does anyone tell me what does it means ??

cin.ignore() reads and discards characters. cin.ignore(80, '\n') is roughly equivalent to this loop:

{
    char ch;

    for (int i = 0; i < 80; i++) {
        if (!cin.get(ch) || ch == '\n')
            break;
    }
}

"Clearing the buffer" means removing pending characters in a line. The reason a line is important is because input from std::cin is nearly always line oriented. You can safely assume that by discarding characters up to and including a newline, that basically ensures that the next character request will force the user to type something.

And also what actually getline() does ?

Really all it does is read characters from the stream up to end-of-file or the specified delimiter and appends them onto the supplied string. A greatly simplified facsimile might look like this:

istream& mygetline(istream& in, string& s, const char delim)
{
    char ch;

    s.clear();

    while (in.get(ch) && ch != delim) {
        if (s.size() == s.max_size()) {
            in.setstate(ios::failbit);
            break;
        }

        s.push_back(ch);
    }

    return in;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

int *sum=0;

sum is a pointer yet you use it as an integer. It shouldn't be a pointer in this case.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Sorry, I thought the question was
how to print '% ' in printf()

That was the question, but if you're going to take it to unreasonable extremes, it's important to bring a little sanity to the thread. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Sorry, couldn't get my CodeBlock IDE to work, so I did it on the ideone.com C compiler.

It'll obviously work, but it's kind of silly. printf() offers a better way to do it, and if you're just printing a string with no replacements then puts() or fputs() would be a better solution anyway:

puts("How are you %dad%");
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Any incorrect or improvement needed?

It's completely broken. I'm fairly confident you didn't attempt to compile that code, much less run it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

for example by adding \" it interprates " as a string then why cannot we use \ in our case?

Because escape characters are interpreted at the compiler level and format specifiers are interpreted at the library level. If \% gave you a literal '%' then it would still be treated as the opening character for a format specifier by the library code. Further, even though the library could be specified to use \%, it would have to actually look like this in user code:

printf("How are you \\%dad\\%");

And the reason is because the compiler will try to interpret any escape, so you need to escape the escape opening character to get a literal '\'. In other words, it's awkward either way, so printf()'s designer took the wiser route, in my opinion, and simply made a format specifier for a literal '%'.

It's probably a little advanced, but you can see a working implementation of printf() here:

http://code.google.com/p/c-standard-library/source/browse/src/internal/_printf.c#35

And the format specifier parsing is done in this function:

http://code.google.com/p/c-standard-library/source/browse/src/internal/_fmtspec.c#128

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

GCC doesn't support void main, therefore any use of it invokes undefined behavior. More specifically, if the compiler doesn't support void main then the code it produces could be very wrong or dangerous.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What have you done so far? We require proof of effort before offering any help.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

ohh... multimap is just an map having second argument as vector , can i say this simply for multimap ?

No, not at all. std::multimap is a collection that supports duplicate keys, it's not the same thing at all as a std::map with a collection as the value.

in C , this is really going to be a difficiult and time taking task so implement our own tree in time constraint ?

If you're writing it from scratch and don't have experience with those data structures then yes. However, if you rely on a good tutorial things become drastically simpler.

the space used in map is the sum of spaces used ny keys + values or does it take more than that ?

Assuming we're talking about a red black implementation then it takes more space. Usually equal to the cost of two or three pointers (links) and a boolean (red black state). There might be other extraneous fields as well.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Buy a beginner's book, read it, and follow the instructions. When you have a specific question or difficulty, feel free to ask here. Daniweb isn't a replacement for learning resources, it's a supplement.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The following is completely unnecessary:

pina=NULL;
pinb=NULL;
pinc=NULL;

free(pina);
free(pinb);
free(pinc);

Calling free() on a null pointer is a no-op, and because you immediately call malloc() for each of those pointers, there's really no purpose in setting them to NULL to begin with.

A glaring problem is that you allocate essentially a 1D array, but then treat it as if it were a 2D array. That's a big no-no, and it's almost as if you're mixing two different methods and breaking them both in the process. Your first initialization loop should look more like this to properly simulate a 2D array (error checking omitted for brevity):

int **pina, **pinb, **pinc;
int i, j;

pina = (int**)malloc(m * sizeof(int*));
pinb = (int**)malloc(m * sizeof(int*));
pinc = (int**)malloc(m * sizeof(int*));

for (i = 0; i < m; i++) {
    pina[i] = (int*)malloc(n * sizeof(int));
    pinb[i] = (int*)malloc(n * sizeof(int));
    pinc[i] = (int*)malloc(n * sizeof(int));
}

Also don't forget to free the inner dimensions. All in all, a fixed add() would look something like this:

void add(int m, int n)
{
    int **pina, **pinb;
    int i, j;

    pina = (int**)malloc(m * sizeof(int*));
    pinb = (int**)malloc(m * sizeof(int*));

    for (i = 0; i < m; i++) {
        pina[i] = (int*)malloc(n * sizeof(int));
        pinb[i] = (int*)malloc(n * sizeof(int));
    }

    printf("Give elements for A: ");
    fflush(stdout);

    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++)
            scanf("%d", &pina[i][j]);
    }

    printf("Give elements for B: "); …
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

strtok() returns a pointer to the same array you're tokenizing, so the instant you read another line you'll have corrupted all previously stored records. Presumably your structure members are pointers, so you can write your own strdup() to make a copy of the token:

/*
    @description:
        Copies the string pointed to by s into a dynamically allocated array.
*/
char *dupstr(const char *s)
{
    char *mem = (char*)malloc(strlen(s) + 1);

    if (mem)
        strcpy(mem, s);

    return mem;
}

Then each token would be saved like so:

Array[i].name = dupstr(strtok(line,";"));

Just be sure to free the memory for each field when you're done with it:

for (i = 0; i < count; ++i) {
    free(Array[i].name);
    free(Array[i].job;
    ...
}

free(Array);
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I already posted in my post that i know this solution of implementing it in one cpp file

Header files are a glorified cut and paste mechanism. If you're having trouble with your headers, then manually creating the same effect in a single file can help highlight the problem. You said you fixed it by using a single file, but clearly you made some sort of change that differs from how your compiler would have done it, otherwise the error would persist even using a single file. Here's how the translation unit would look with your headers directly included:

#include<iostream>
using namespace std ;
class FileOne ;
class FileTwo
{
public:
    int Test(FileOne One){
        return (One.var1+One.var2);}
    FileTwo(void);
    ~FileTwo(void);
};
class FileTwo ;
class FileOne
{
private:
    int var1 , var2 , var3 ;
public :
    friend int FileTwo::Test(FileOne One);
    FileOne(){
        var1= 12;var2 = 24;
    }
};
int main(){
    FileOne one ;
    FileTwo two ;
    cout<<two.Test(one);
}

The error is still present, and it's much easier to see now that everything is all together in one file. Once you fix the error in a single file, you can break it back up into multiple files. And in this case, the fix is to separate the member function declarations from their corresponding definitions (ie. use a header/implementation file setup).

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

A hardware barcode reader will give you the contents of the barcode, so there's nothing barcode specific you need to do in your program. Just accept standard input as if it came from a keyboard.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

When you forward declare a class, you only tell the compiler that it exists, not what's in it. So while the declarations are fine, the definition of FileTwo::Test() would need to be deferred until after both classes have been introduced completely:

class FileOne;

class FileTwo
{
public:
    int Test(FileOne One);
};

class FileTwo;

class FileOne
{
private:
    int var1, var2, var3;
public:
    friend int FileTwo::Test(FileOne One);

    FileOne()
    {
        var1 = 12;
        var2 = 24;
    }
};

// Now this definition will work
int FileTwo::Test(FileOne One)
{
    return (One.var1 + One.var2);
}

#include <iostream>

using namespace std;

int main()
{
    FileOne one;
    FileTwo two;

    cout << two.Test(one) << '\n';
}

To facilitate this with multiple files and headers, I'd suggest separating your class declarations and definitions into a header and implementation file. First the header:

// FileTwo.h
#ifndef FILETWO_H
#define FILETWO_H

class FileOne;

class FileTwo
{
public:
    int Test(FileOne One);
};

#endif

Then the implementation file which handles definitions:

// FileTwo.cpp
#include "FileOne.h"
#include "FileTwo.h"

int FileTwo::Test(FileOne One)
{
    return (One.var1 + One.var2);
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

A function prototype is also called a function declaration. You supplied a function definition.

A definition is also a declaration, but a declaration is not always a prototype. A prototype is the very specific declaration without a body that defines parameters (excluding the case of an empty parameter list), also referred to by the standard as a "prototype declaration":

void foo(void); // No arguments
int max(const int a, const int b);

A prototype-like declaration with empty parentheses is not a prototype:

void foo(); // Declaration (unknown arguments), not a prototype

Definitions introduce a declaration, but follow the same rules and only introduce a prototype if the declaration alone would do so:

// Definition (no arguments), also a prototype (no arguments)
void foo(void) { }

// Definition (no arguments), also a declaration (unknown arguments), not a prototype
void bar() { }

kw42chan's code was correct as far as technically introducing a prototype even though it was done through a definition. But I suspect that it's not correct in terms of the spirit of the assignment, which is to write a prototype independent of the definition:

int max(int a, int b); // Prototype

int main(void)
{
    ...
}

int max(int a, int b) // Definition
{
    ...
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

how does it hash each value to a differnet location ?

It doesn't hash anything, the keys are exact matches. For duplicate keys in C++ you'd either use a multimap<> or store a list of values at the same key:

map<string, vector<T>> m;

In C you'd do something similar, but using a third party library or by writing your own tree.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'd suggest reading each line with fgets() and then parsing it in memory to extract fields. You can do the parsing with sscanf(), though it can get a little tricky unless you guarantee that the file ends with a newline character, because you need to consider fields that end in all three of ';', '\n', and EOF:

#include <stdio.h>
#include <string.h>

int main(void)
{
    FILE *in = fopen("test.txt", "r");

    if (in) {
        char line[BUFSIZ];

        while (fgets(line, sizeof line, in)) {
            size_t pos = 0, n;
            char field[80];

            /* Trim the newline as it tends to cause trouble */
            line[strcspn(line, "\n")] = '\0';

            while (sscanf(line + pos, "%79[^;]%n", field, &n) == 1) {
                puts(field);
                pos += n;

                if (line[pos] == ';')
                    ++pos;
            }
        }

        fclose(in);
    }

    return 0;
}

If the file is guaranteed to end with a newline, the EOF case goes away and things become drastically simpler as you can do all of the work for the other two cases in the sscanf() format string:

#include <stdio.h>
#include <string.h>

int main(void)
{
    FILE *in = fopen("test.txt", "r");

    if (in) {
        char line[BUFSIZ];

        while (fgets(line, sizeof line, in)) {
            size_t pos = 0, n;
            char field[80];

            while (sscanf(line + pos, "%79[^;\n]%*[;\n]%n", field, &n) == 1) {
                puts(field);
                pos += n;
            }
        }

        fclose(in);
    }

    return 0;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Just provide a public property in form two that the button click event sets in form one. The setter for the property would invalidate the textbox in form two and cause it to redraw. Easy peasy. And no, I won't give you the code for it because this is clearly homework and you haven't show any proof of effort.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

While I don't disagree that VBA is missing a solid home, I'm not convinced that we need to add a new forum when VBA threads can be tagged as such and placed in a forum that's "close enough" and likely to be seen by fellow VBA peeps.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

anyone can explain to me why const is needed here???

It's not needed, but if the function shouldn't reseat the pointer then adding const to enforce that behavior isn't a bad thing. Generally const is there to keep you as the programmer from doing something stupid. It's hard to remember the intended limitations of every object your code uses. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

don't know how map work ?

Typically it's some form of balanced binary search tree, with red black trees being the most common implementation.

secondly, can i say that it is good to learn C++ for these types of implementaions ?

Using a library is recommended over rolling your own data structures. Whether that results in using a language where the standard library has what you need or linking with a third party library so that you can continue using C is entirely your decision.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

An example is mentioned specifically in the form of prepending size information to the block of memory. If the object is small, the overhead of size information can be prohibitive. It's only when the allocated objects get bigger that the "bookkeeping" information gets amortized away.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster
tux4life commented: Yuppers ;-) +13
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

i think d code is wrong

Can you be more specific? :rolleyes:

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

.NET programs aren't compiled to machine code as part of the assembly, they're in an intermediate state (IL assembly language) that the framework needs to compile into machine code. On the first run of an assembly, the just-in-time (JIT) compiler does all of that, which is why it's slower than subsequent executions.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

On IDEs I would vote for CodeLite.

Seconded. If you find Visual C++ too heavy then CodeLite is a great alternative to the more oft suggested Code::Blocks on Windows. I'd still recommend Visual Studio if you're on Windows as it's more likely to be used in the professional arena, and experience with the tool will improve your marketability slightly.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Thank you! I'm feeling so stupid...

17 years programming C and I still make that particular mistake (ie. using = instead of ==). No worries, it's a common and easy one to make. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

at this level and the experience which i have , what can i do so that i develop skills which is needed in a PROFESSIONAL programmer ?

You could probably get in on some open source projects.

P.S i was thinking that i will be professional when i will join any big company like amazon, google or somthing like that. was i right in my thinking ?

You're a professional when you get paid to program. I'd further refine it into being a professional if you make a living off of programming, but that's just my opinion.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The "console screen" isn't unique to Turbo C. Your suggestion is still very poor and makes unwarranted assumptions about the OP's compiler and operating system.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

@deceptikon: The November update of MSVC11 did add support for, most notably: variadic templates, initializer-lists, and delegating constructors.

Ah, I wasn't aware it was a separate install. That's fantastic. So VC11 is pretty much there in terms of conformance as far as I'm concerned. Now it's just little things. :)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

you can use gotoxy() function to draw output

gotoxy() isn't widely implemented by compilers. You're essentially assuming that the OP is using Turbo C, which isn't a safe assumption.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

By any chance, were you guys trying to come up for a new update for the site?

The site is updated pretty much daily. But yes, there was a semi-new feature added yesterday in the form of badge linkbacks.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

At this point I've lost interest in helping you because you keep asking essentially the same question and have still shown absolutely zero effort. I suspect nothing will come of this project and as such don't feel like wasting my own time designing it for you.

But one last time before I abandon the thread: Figure out what you want the system to do, then write code that does it. If you don't have any clue how things are supposed to work, writing software to do it is impossible.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Sadly, I'm unable to touch the production server in an administrative capacity. But looking at recent code changes, it looks like the fix will be as simple as a file rollback from version control.