COKEDUDE 27 Junior Poster in Training

I was looking at this struct tutorial.

https://www.tutorialspoint.com/cprogramming/c_structures.htm

It mentions "The structure tag is optional". How would you refer to your struct if there is no structure tag?

struct {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};  

//something like this? 
struct random_name;
COKEDUDE 27 Junior Poster in Training

How would you describe c programming to a person that does not know anything about computers?

COKEDUDE 27 Junior Poster in Training

I sometimes move my code around to various computers so I prefer to keep my header files in my current directory. Unfortunately it does not seem to be working.

$ gcc *.c -Wall
main.c:1:10: fatal error: mysql.h: No such file or directory
    1 | #include <mysql.h>
      |          ^~~~~~~~~
compilation terminated.
COKEDUDE 27 Junior Poster in Training

I am trying to replace a string in a function with double pointers. I would like to see this change back in main. What am I doing wrong?

These are the warnings my compiler gave me.

$ gcc *.c -Wall
main.c: In function ‘replacevalue’:
main.c:10:12: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
     strcpy(**pinput1, string);
            ^
In file included from main.c:4:0:
/usr/include/string.h:38:8: note: expected ‘char * restrict’ but argument is of type ‘char’
 char  *strcpy (char *__restrict, const char *__restrict);
        ^~~~~~

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

void replacevalue(char **pinput1)
{
    char string[] = "here is the replacement string";
    strcpy(**pinput1, string);
}

void stringtest(char *pinput1)
{
    replacevalue(&pinput1);
}

int main()
{
    char string[] = "here is a string                          ";
    char *pinput1 = string;
    printf("%s \n", pinput1);
    stringtest(pinput1);
    printf("%s \n", pinput1);

    return 0;
}//end main
COKEDUDE 27 Junior Poster in Training

I am doing a team project with some people that prefer Linux and some people prefer Windows. We are also not allowed to share code for security reasons. One thing the Windows people love to whine about is a way to hold the window open. So the way they usually solve the problem is with:

#include <conio.h>
getch(); 

This creates a problem for the Linux users since we do not have that library. I have been looking for ways to solve this problem and I really like this idea.

https://www.dreamincode.net/forums/topic/30581-holding-the-execution-window-open/page__st__30__p__575874entry575874

#include <stdlib.h> //For system commands

//Detect and define which command to use
#ifndef WIN32
    #define COMMAND "clear" //Clears a linux console screen
#else
    #define COMMAND "cls" //Clears a windows console screen
#endif

#define wipe() system( COMMAND )
//To clear the console screen use wipe();

#define wait() {printf("Hit \"Enter\" to continue\n");fflush(stdin);getchar();fflush(stdin);}
//To pause the program or hold the console screen open use wait();

This seems to work when I use #defines but not when I just use this line:

printf("Hit \"Enter\" to continue\n");fflush(stdin);getchar();fflush(stdin);

Since I know most of the programmers pretty well I know they use scanf so I added this and it worked:

while ((getchar()) != '\n'); 

Why did the #defines work without the extra while loop, but I needed the extra while loop when not using the #defines? Do #defines behave a little different? I can not look at the others peoples code unfortunately. Is there a better way to solve the problem …

COKEDUDE 27 Junior Poster in Training

How do you make a variable global to a scope? I only know about global variables to an entire program where you put it at the top of the program then your entire program can see it.

COKEDUDE 27 Junior Poster in Training

I have 30 functions like this

void func1(int *var1, int *var2)
{
    func2(var1, var2);
}

void func2(int *var1, int *var2)
{
    func3(var1, var2);
}

void func3(int *var1, int *var2)
{
    func4(var1, var2);
}

I do not need var3 until func15. Is there a way to fix this mistake without having to change all the function calls and headings? var3 needs to be remembered every time I get to func15. I only use var3 in func15 though so I really do not want to have to change all of my function calls and headings. It obviously can't be a local variable or it will be forgotten each time it is called.

void func15(int *var1, int *var2, int *var3)
{
    func16(var1, var2);
}
COKEDUDE 27 Junior Poster in Training

If you have a counter that gets incremented through multiple functions, is it better to use a global counter or pointer counter? They both work just curious which is better and why.

Johny_2 commented: ffgfvfr +0
COKEDUDE 27 Junior Poster in Training

I am trying to a % in a printf statement represent 10%. How do I do that? My compiler warnings are complaining about it. I tried a backslash but that didn't work.

printf(" 10\% of people  \n");
COKEDUDE 27 Junior Poster in Training

What method would code police like? I can't think of any other way to do this but one of these methods.

The wall warning of gcc doesn't like the third method some reason.

 warning: argument to ‘sizeof’ in ‘memset’ call is the same expression as the destination; did you mean to provide an explicit length? [-Wsizeof-pointer-memaccess]
COKEDUDE 27 Junior Poster in Training

Which method do you usually use?

buffer[0][0] = '\0';
memset(buffer,0,strlen(buffer[0]));
memset(buffer,0,sizeof(buffer[0]));
rproffitt commented: Item 1. But Code Police may issue a ticket. +14
COKEDUDE 27 Junior Poster in Training

I am the master of segmentation faults when doing linked lists so I don't really like this option.

Setting to null sounds easy :).

I only have an array of size 300 so not really worried about this.

COKEDUDE 27 Junior Poster in Training

Whats the best way to removes values from array of strings? Is it better to create a new array of strings or is there some way to remove them?

COKEDUDE 27 Junior Poster in Training

This is C programming not PHP.

COKEDUDE 27 Junior Poster in Training

Like this?

if(!(row1 = mysql_fetch_row(result1)) && (adding_to_member_flag != 1))
{
}
COKEDUDE 27 Junior Poster in Training

Why does order matter with this if else if statement?

if((row1 = mysql_fetch_row(result1)) && adding_to_member_flag != 1)
{
    printf("Do not update date since it already exists\n");

}
else if(!(row1 = mysql_fetch_row(result1)) && adding_to_member_flag != 1)
{
    printf("Update end_date and add new team\n");
}

When I had it like this the "Do not update date since it already exists" never got printed.

if(!(row1 = mysql_fetch_row(result1)) && adding_to_member_flag != 1)
{
    printf("Update end_date and add new team\n");
}
else if((row1 = mysql_fetch_row(result1)) && adding_to_member_flag != 1)
{
    printf("Do not update date since it already exists\n");

}
COKEDUDE 27 Junior Poster in Training

"is" instead of "=".

COKEDUDE 27 Junior Poster in Training

I am trying to set a record back to null. I was able to change it from null and now I want to change it back to null. For some reason it is not working.

This worked perfectly fine:

UPDATE  members set end_date = "2017-01-18" where player = 1 and end_date is NULL;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1  Changed: 1  Warnings: 0

This is not working.

UPDATE  members set end_date = NULL where player = 1 and end_date is "2017-01-18";
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"2017-01-18"' at line 1
COKEDUDE 27 Junior Poster in Training

I am trying to print my data in columns. For some reason every column but the last one lines up. Is there a way to fix this?

while ((row = mysql_fetch_row(result))) 
{ 
    //printf("198\n");
    for(int i = 0; i < num_fields; i++) 
    {
        //printf("----------\n");           
        printf("%25s         ",row[i]);
        if(row[i] == NULL)
        {
            printf("No values exist\n");
        }
        //printf("----------\n");
        //printf("%s      ", row[i] ? row[i] : "NULL"); 
    } 
        printf("\n"); 
}

My Output:

             Sang Hun Lee                             VINES                                KR
            Jung Hyun Lim                             Crazy                                KR
           Tom Brekelmans                              PoYo                                NL
            Shin Jung Min                         Superstar                                KR
         Lacroix Corentin                           Makouni                                BE
           Beak Seung-Joo                   ButterflyEffect                                KR
           Choi Joon Hyuk                                xD                                KR
            Kwun Jung Woo                              Fake                                KR
       Kalle Pietiläinen                          Prospect                                SE
COKEDUDE 27 Junior Poster in Training

I want the loop to continue until sscanf_count equals 2 or the user hits ctrl + D.

COKEDUDE 27 Junior Poster in Training

My second while condition is being ignored and I don't understand why. I would think I have 1 and 0 = 0 so it would break out.

while(fgets(line, sizeof line, stdin) && sscanf_counter != 2)
{
}
COKEDUDE 27 Junior Poster in Training

What does this printf do? Not familiar with the question mark, colon, and extra NULL at the end.

printf("%s      ", row[i] ? row[i] : "NULL"); 
COKEDUDE 27 Junior Poster in Training

I thought newline is equivalent to hitting enter on the keyboard. Is that correct? With this while loop it does not stop when I hit enter. I have to hit Ctrl-D or fill up the buffer to make it stop reading.

while(fgets(line, sizeof line, stdin))
{
}
COKEDUDE 27 Junior Poster in Training

When using fgets to read from stdin how do you stop reading from stdin?

COKEDUDE 27 Junior Poster in Training

Can you please explain why?

COKEDUDE 27 Junior Poster in Training

Can you tell me what I did wrong here then?

https://ideone.com/RylYp1

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

/*
 * Given a string which might contain unescaped newlines, split it up into
 * lines which do not contain unescaped newlines, returned as a
 * NULL-terminated array of malloc'd strings.
 */
char **split_on_unescaped_newlines(const char *txt) {
    const char *ptr, *lineStart;
    char **buf, **bptr;
    int fQuote, nLines;

    /* First pass: count how many lines we will need */
    for ( nLines = 1, ptr = txt, fQuote = 0; *ptr; ptr++ ) {
        if ( fQuote ) {
            if ( *ptr == '\"' ) {
                if ( ptr[1] == '\"' ) {
                    ptr++;
                    continue;
                }
                fQuote = 0;
            }
        } else if ( *ptr == '\"' ) {
            fQuote = 1;
        } else if ( *ptr == '\n' ) {
            nLines++;
        }
    }

    buf = malloc( sizeof(char*) * (nLines+1) );

    if ( !buf ) {
        return NULL;
    }

    /* Second pass: populate results */
    lineStart = txt;
    for ( bptr = buf, ptr = txt, fQuote = 0; ; ptr++ ) {
        if ( fQuote ) {
            if ( *ptr == '\"' ) {
                if ( ptr[1] == '\"' ) {
                    ptr++;
                    continue;
                }
                fQuote = 0;
                continue;
            } else if ( *ptr ) {
                continue;
            }
        }

        if ( *ptr == '\"' ) {
            fQuote = 1;
        } else if ( *ptr == '\n' || !*ptr ) {
            size_t len = ptr - lineStart;

            if ( len == 0 ) …
COKEDUDE 27 Junior Poster in Training

This is what I came up with for reading a csv file with multiple types. It seems to get the job done in all cases but 1,2,,"a". Where there is a blank space. Can I please have some ideas on how to fix this?

const char* getfield(char* line, int num)
{
const char* tok;
for (tok = strtok(line, ",");
        tok && *tok;
        tok = strtok(NULL, ",\n"))
{
    if (!--num)
        return tok;
}
return NULL;
}

while(fgets(line, 80, inputfp1) != NULL)
{
    printf(" line is %s \n", line);

    char* tmp1 = strdup(line);
    char* tmp2 = strdup(line);
    char* tmp3 = strdup(line);
    char* tmp4 = strdup(line);
    printf("Field 1 would be %s\n", getfield(tmp1, 1));
    printf("Field 2 would be %s\n", getfield(tmp2, 2));
    printf("Field 3 would be %s\n", getfield(tmp3, 3));
    printf("Field 4 would be %s\n", getfield(tmp4, 4));
    // NOTE strtok clobbers tmp
    free(tmp1);
    free(tmp2);
    free(tmp3);
    free(tmp4);

    //sscanf(line, "%d, %d, %d, %d", &column1[i], &column2[i], &column3[i], &column4[i]);
    //printf(" column1[i] is %d column2[i] is %d column3[i] is %d column4[i] is %d \n", column1[i], column2[i], column3[i], column4[i]);
    i++;
    memset(line, 0, 80);
}
COKEDUDE 27 Junior Poster in Training

How would I split this big if statement across multiple lines? It works perfectly on one line.

if (mysql_query(con, "CREATE TABLE Matches(match_id INT UNSIGNED PRIMARY KEY, date DATETIME NOT NULL, tournament INT UNSIGNED, playerA INT UNSIGNED, playerB INT UNSIGNED, scoreA INT UNSIGNED, scoreB INT UNSIGNED, offline bool)"))
{
    finish_with_error(con);
}

When I split it then it does not work.

if (mysql_query(con, "CREATE TABLE Matches(match_id INT UNSIGNED PRIMARY KEY, date DATETIME NOT NULL, 
tournament INT UNSIGNED, playerA INT UNSIGNED, playerB INT UNSIGNED, scoreA INT UNSIGNED, scoreB INT UNSIGNED, offline bool)"))
{
    finish_with_error(con);
}
COKEDUDE 27 Junior Poster in Training

I keep getting this message.

undefined reference to `mysql_get_client_info'

From what I can figure out when I google it I am not linking correctly. So I tried this.

gcc *.c -L"C:\Program Files\MySQL\MySQL Server 5.7\lib"

Can I please get some help on what I am doing wrong?

COKEDUDE 27 Junior Poster in Training

I'm not using Ubuntu. I'm using Windows. Those are deb packages which are for Ubuntu.

COKEDUDE 27 Junior Poster in Training

How do you download them? I only see a list of files.

COKEDUDE 27 Junior Poster in Training

I'm using windows so I installed Pelles like it suggested.

COKEDUDE 27 Junior Poster in Training

Are there any good tutorials c programming with mysql? I only found one promising link, unfortunately they neglect to to show what is in their header files which doesn't help very much.

http://zetcode.com/db/mysqlc/

COKEDUDE 27 Junior Poster in Training

I have four array of strings. I would like to print out the strings with this formatting:

                        printf(" %d \t %s \t %s \t %s \t %s.\n",
                        quadruples_line_counter,
                        strings_quadruples1_action[0],
                        strings_quadruples2_variable1[0],
                        strings_quadruples3_variable2[0],
                        strings_quadruples4_temp_variable[0]);

It gives this output:

 17      func    sub     int     3.
 17      param   (null)          (null)          (null).
 17      alloc           4       (null)          xa.
 17      alloc           4       (null)          y.
 17      alloc           4       (null)          z.
 17      multiply        55      y       t0.
 17      divide          t0      z       t1.
 17      plus            xa      t1      t2.
 17      plus            t2      x       t3.
 17      func    main    void    0.
 17      alloc           4       (null)          a.
 17      alloc           4       (null)          b.
 17      alloc           4       (null)          c.
 17      arg             (null)          (null)          x.
 17      arg             (null)          (null)          y.
 17      arg             (null)          (null)          z.
 17      call            sub     3       t5.
 17      assign          t5      (null)          y.

How would I go about ignoring the nulls when printing? I am not sure how to do this.

COKEDUDE 27 Junior Poster in Training

Since there is no edit button I'm updating my code here.

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

int main(int argc, char *argv[])
{

    char str [] = "xa_55_y_*_z_/_+_x_+";
    int length = 0;
    int decrementor = 0;
    int underscore_counter = 0;
    int i = 0;
    length = strlen (str);

    for(i  = 0; i < length; i++)
    {
        decrementor = 0;
        underscore_counter = 0;
        if(str[i] == '*' || str[i] == '/' || str[i] == '+' || str[i] == '-')
        {
            decrementor = i;
            while(underscore_counter != 3)
            {
                if(str[decrementor] == '_')
                {
                    underscore_counter++;
                    printf("underscore_counter is %d \n", underscore_counter);
                }
                decrementor--;
            }
        }
    }

    return 0;
}
COKEDUDE 27 Junior Poster in Training

In the str char array below I would first like to locate the first math symbol I see, then I would like to count backwards and remove whatever is between the previous three " _ " and remove the three " _ ". Can I please get some ideas on how to do this?

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

int main(int argc, char *argv[])
{

    char str [] = "xa_55_y_*_z_/_+_x_+";
    int length = 0;
    int decrementor = 0;
    int underscore_counter = 0;
    int i = 0;
    length = strlen (str);

    for(i  = 0; i < length; i++)
    {
        decrementor = 0;
        underscore_counter = 0;
        if(str[i] == '*' || str[i] == '/' || str[i] == '+' || str[i] == '-')
        {
            decrementor = i;
            while(underscore_counter != 3)
            {
                if(str[i] == '_')
                {
                    underscore_counter++;
                }
                decrementor--;
            }
        }
    }

    return 0;
}
COKEDUDE 27 Junior Poster in Training

How does this compile? I forgot to set my second condition in my second for loop and it compiles.

    if(cmp_str5 == 0 || cmp_str6 == 0)
    {
        if(cmp_str5 == 0 || cmp_str6)
COKEDUDE 27 Junior Poster in Training

I am trying to add an int to a Multi-dimensional char Array. After reading the link below I would think I can use sprintf. If I can't use sprintf what is another way I can do this?

http://www.cplusplus.com/reference/cstdio/sprintf/

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


int main(int argc, char *argv[])
{
    //{"TYPE", "ID", "SCOPE", "VALUE"}
    char *symbol_table_variables[503][4];
    int scope = 0;
    int lower_bound_of_big_boy_counter = 0;
    sprintf (symbol_table_variables[lower_bound_of_big_boy_counter][2], "%d", scope);
    printf("symbol_table_variables[lower_bound_of_big_boy_counter][2] %s \n", 
    symbol_table_variables[lower_bound_of_big_boy_counter][2]);
    return 0;
}
COKEDUDE 27 Junior Poster in Training

How would I go about making this table?

ID    Type   Value
X     int    4
Y     int    4
Z     float  0

This is the only way I can think of but I don't think this will work.

int main(int argc, char *argv[])
{
    char *strings_line1[3] = {"ID", "TYPE", "Value"};
    char *strings_line2[3] = {"X", "int", "4"};
    char *strings_line3[3] = {"Y", "int", "4"};
    char *strings_line4[3] = {"Z", "float", "0"};
    return 0;
}

Is this considered a symbol table? Whats the difference between a table and symbol table?

COKEDUDE 27 Junior Poster in Training

My understanding of code blocks is that it uses gcc. So I am curious why gcc allows you to have a function called itoa, but code blocks does not let you have a function called itoa. Does anyone know?

COKEDUDE 27 Junior Poster in Training

I always get confused with while when I need to use multiple condition. I believe this is correct.

1 and 1 = 1
1 and 0 = 0
1 or 1 = 1
1 or 0 = 1

Is there something special I need to do with while loops? If statements always seem to behave like I expect with multiple conditions. Is there a good rule of thumb to follow?

Like here. Since I usually try || and it never seems to work I used && first. But Of course I needed an || in this case just because I did the opposite of what I usually do.

const char *s = buffer;
parser_comment_level = 0
while (ispunct((unsigned char)*s) || parser_comment_level != 0)
{
}

I would like to change the above to. Is that correct or do I need to use an &&? Would it be a good idea to use parenthesis and if so how?

const char *s = buffer;
parser_comment_level = 0
while (ispunct((unsigned char)*s) || parser_comment_level != 0 || (unsigned char)*s != '\0')
{
}
COKEDUDE 27 Junior Poster in Training

I am trying to get the first value of a null initialed array of string.I have tried every method I can think of with no luck. I either get warning, errors, or a segmentation fault. Whats the correct way to print the null value and use it in the if statement?

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

int main(int argc, char** argv) 
{
    char *strings_line_tokens[503] = {0};
    strings_line_tokens[0] = malloc(strlen("cookies")+1);
    strcpy(strings_line_tokens[0], "cookies");
    printf("strings_line_tokens[]: %s\n",strings_line_tokens[0]);
    printf("-------------------\n");
    printf("strings_line_tokens[]: %c %d \n",(char)strings_line_tokens[1], (int)strings_line_tokens[1]);
    printf("aaaaaaaaaaaaaaaa\n");
    printf("strings_line_tokens[]: %c %d \n",strings_line_tokens[1], strings_line_tokens[1]);
    printf("bbbbbbbbbbbbbbbb\n");
    printf("strings_line_tokens[]: %c %d \n",(char)strings_line_tokens[1][0], (int)strings_line_tokens[1][0]);
    printf("ccccccccccccccccccc\n");
    printf("strings_line_tokens[]: %c %d \n",strings_line_tokens[1][0], strings_line_tokens[1][0]);
    printf("-------------------\n");
    if(strings_line_tokens[1] == 0)
    {
        printf("You have a NULL \n");
    }
    return 0;
}

Here are the current warning.

main.c:13: warning: cast from pointer to integer of different size
main.c:13: warning: cast from pointer to integer of different size
main.c:15: warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘char *’
main.c:15: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘char *’
COKEDUDE 27 Junior Poster in Training

In this example in the malloc() what does the UL do?

#include <stdio.h>        /* perror */
#include <errno.h>        /* errno */
#include <stdlib.h>       /* malloc, free, exit */

int main(void)
{

    /* Pointer to char, requesting dynamic allocation of 2,000,000,000
     * storage elements (declared as an integer constant of type
     * unsigned long int). (If your system has less than 2 GB of memory
     * available, then this call to malloc will fail.)
     */
    char *ptr = malloc(2000000000UL);

    if (ptr == NULL) {
        perror("malloc failed");
        /* here you might want to exit the program or compensate
           for that you don't have 2GB available
         */
    } else {
        /* The rest of the code hereafter can assume that 2,000,000,000
         * chars were successfully allocated... 
         */
        free(ptr);
    }

    exit(EXIT_SUCCESS); /* exiting program */
}
COKEDUDE 27 Junior Poster in Training

I have seen a few different ways of doing malloc error checking? Is one way better than the other? Are some exit codes better than others? Is using fprintf with stderr better than using a printf statement? Is using a return instead of an exit better?

    ptr=(int*)malloc(n*sizeof(int));  //memory allocated using malloc
    if(ptr==NULL)                     
    {
        printf("Error! memory not allocated.");
        exit(0);
    }

    ptr=(int*)malloc(n*sizeof(int));  //memory allocated using malloc
    if(ptr==NULL)                     
    {
        printf("Error! memory not allocated.");
        exit(1);
    }

    res = malloc(strlen(str1) + strlen(str2) + 1);
    if (!res) {
        fprintf(stderr, "malloc() failed: insufficient memory!\n");
        return EXIT_FAILURE;
    }

    ptr=(int*)malloc(n*sizeof(int));  //memory allocated using malloc
    if(ptr==NULL)                     
    {
        printf("Error! memory not allocated.");
        exit(-1);
    }

   ptr=(int*)malloc(n*sizeof(int));  //memory allocated using malloc
    if(ptr==NULL)                     
    {
        printf("Error! memory not allocated.");
        exit(EXIT_FAILURE);
    }

char *ptr = (char *)malloc(sizeof(char) * some_int);
if (ptr == NULL) {
    fprintf(stderr, "failed to allocate memory.\n");
    return -1;
}

char* allocCharBuffer(size_t numberOfChars) 
{
    char *ptr = (char *)malloc(sizeof(char) * numberOfChars);
    if (ptr == NULL) {
        fprintf(stderr, "failed to allocate memory.\n");
        exit(-1);
    }
    return ptr;
}
COKEDUDE 27 Junior Poster in Training
COKEDUDE 27 Junior Poster in Training

Is this the correct way to print char*? *I know c has the value I want because of the print statement below it. The o is not giving the value that I expect.

        char   out[50];
        char  *o = out;
        int    i = 118;
        *o++ = c;
        printf("o is %d (%c)\n", o[0], o[0]);
        printf("o is %d (%c)\n", o[1], o[1]);
        printf("o is %d (%c)\n", o[2], o[2]);
        printf("c is %d \n", c);
COKEDUDE 27 Junior Poster in Training

I need some ideas for dealing with array of strings. ptr contains my array of strings. The goal here is to combine certain strings when the conditions of the for loop are met. The problem is I need remove the strings that get combined with another string so I don't reuse that string later on.

The ways I thought of so far.
1. Create a temporary string array(array of char null terminated). Then combine them into the temporary string array. Then copy the temporary string array into the array of strings. After this I need to remove the the strings that got combined. The only way I can of doing this is to shift everything to the appropriate location to overwrite the strings I want to remove. I could be combining up to 5 strings at a time so I think this could be tricky.

  1. Create another array of strings. Copy the data from the original array of strings into the new array of strings as I increment through the for loop. When I need to combine strings copy them into the temporary string array. Then copy the temporary string array into the new array of strings. This way seems safer.

Is there a better way to do this? I really don't like either method.

for(x = 0; x < n; x++)
{
    if(isdigit(ptr[x][0]))
    {
        printf("ptr[x] is %s \n", ptr[x]);
        x++;
        if(ptr[x][0] == 46)
        {
            printf("ptr[x] is %s \n", ptr[x]);
            x++;
            if(isdigit(ptr[x][0]))
            {
                printf("ptr[x] is %s \n", …
COKEDUDE 27 Junior Poster in Training

I'm not getting the expected result when I do division. Can someone please tell me what I'm doing wrong?

int page_list_size = 20; 
int page_fault_counter = 0;
double failure = 0.0;
double success = 0.0;
failure = page_list_size / page_fault_counter;
success = 1 - failure;
printf("failure Is %lf\n",failure);
printf("success Is %lf\n",success);
COKEDUDE 27 Junior Poster in Training

After you have the appropriate infix to postfix expression how would you do the math with the expression? I'm having difficulty programming it.

// Operator supported: +,-,*,/,%,^,(,)
// Operands supported: all single character operands

#include<stdio.h>
#include<conio.h>
#include<ctype.h>

#define MAX 50

typedef struct stack
{
    int data[MAX];
    int top;
}stack;

int precedence(char);
void init(stack *);
int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *,int);
int top(stack *);   //value of the top element
void infix_to_postfix(char infix[],char postfix[]);

void main()
{
    char infix[30],postfix[30];
    printf("Enter an infix expression(eg: 5+2*4): ");
    gets(infix);
    infix_to_postfix(infix,postfix);
    printf("\nPostfix expression: %s",postfix);
}

void infix_to_postfix(char infix[],char postfix[])
{
    stack s;
    char x,token;
    int i,j;    //i-index of infix,j-index of postfix
    init(&s);
    j=0;

    for(i=0;infix[i]!='\0';i++)
    {
        token=infix[i];
        if(isalnum(token))
            postfix[j++]=token;
        else
            if(token=='(')
               push(&s,'(');
        else
            if(token==')')
                while((x=pop(&s))!='(')
                      postfix[j++]=x;
                else
                {
                    while(precedence(token)<=precedence(top(&s))&&!empty(&s))
                    {
                        x=pop(&s);
                        postfix[j++]=x;
                    }
                    push(&s,token);
                }
    }

    while(!empty(&s))
    {
        x=pop(&s);
        postfix[j++]=x;
    }

    postfix[j]='\0';
}

int precedence(char x)
{
    if(x=='(')
        return(0);
    if(x=='+'||x=='-')
        return(1);
    if(x=='*'||x=='/'||x=='%')
        return(2);

    return(3);
}

void init(stack *s)
{
    s->top=-1;
}

int empty(stack *s)
{
    if(s->top==-1)
        return(1);

    return(0);
}

int full(stack *s)
{
    if(s->top==MAX-1)
        return(1);

    return(0);
}

void push(stack *s,int x)
{
    s->top=s->top+1;
    s->data[s->top]=x;
}

int pop(stack *s)
{
    int x;
    x=s->data[s->top];
    s->top=s->top-1;
    return(x);
}

int top(stack *p)
{
    return (p->data[p->top]);
}
COKEDUDE 27 Junior Poster in Training

Can someone fix my title? I don't see an edit option.

warning: passing argument 1 of 'cat' from incompatible pointer type [enabled by default]