Ετήσια αρχεία: 2016


C/C++: How NOT to define multiple pointers

When defining pointers in C/C++ you should be careful on how you use the * characters. If you try to define multiple pointers on the same line and you do not add the * character in front of each variable, then the results will not be what you would expect. In the following examples we added the * to the data type definition, hoping that all variables would become pointers of that data type. Unfortunately, as the compiler points out later on while making the comparisons, only the first variable in each line becomes a pointer of the data type.

Wrong Examples

C Source Code:

#include <stdio.h>

int main()
{
    int* a, b;
    int *c, d;
    int * e, f;

    a == b;
    c == d;
    e == f;

    return 0;
}

Compiler Output:

$ gcc -o main *.c                                                                                          
main.c: In function 'main':                                                                                      
main.c:9:7: warning: comparison between pointer and integer                                                      
     a == b;                                                                                                     
       ^                                                                                                         
main.c:10:7: warning: comparison between pointer and integer                                                     
     c == d;                                                                                                     
       ^                                                                                                         
main.c:11:7: warning: comparison between pointer and integer                                                     
     e == f;                                                                                                     
       ^                                                                                                         

C++ Source Code:

#include <iostream>
using namespace std;

int main()
{
    int* a, b;
    int *c, d;
    int * e, f;

    a == b;
    c == d;
    e == f;

   return 0;
}

Compiler Output:

$ g++ -std=c++11 *.cpp -o main

main.cpp: In function ‘int main()’:
main.cpp:10:10: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     a == b;
          ^
main.cpp:11:10: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     c == d;
          ^
main.cpp:12:10: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     e == f;
          ^

Correct Examples

#include <stdio.h>

int main()
{
    int* a, * b;
    int *c, *d;
    int * e, * f;

    a == b;
    c == d;
    e == f;

    return 0;
}
#include <iostream>
using namespace std;

int main()
{
    int* a, * b;
    int *c, *d;
    int * e, * f;

    a == b;
    c == d;
    e == f;

   return 0;
}

MySQL Workbench: A solution to Error Code 1175 2

While using MySQL Workbench, we tried to execute an UPDATE command that did not use the Key of the table in the WHERE clause. In our case, it did not matter and it was expected to perform that update without that restriction.

When we tried to execute the code though, we got the following error:

Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

and the following solution from Workbench:

To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.

We did not want to close the session for many reasons (we had data in other tabs, we would lose the undo history, we would have to revert the change later on, etc.).

To solve this issue without reconnecting nor changing the configuration of the tool, we appended the following code above the update statement and executed both lines together (we selected the two commands and pressed CTRL+Shift+Enter):

SET SQL_SAFE_UPDATES = 0;

This line of code, instructed the tool to ignore Error 1175 and allowed us to complete our task.


Java: Automatically get Enum from String

The following code will create a static map that contains entries where the key would be the string representation of the enum enum.toString() and value would be the enum itself. This way you can retrieve an enum from its string representation using the static fromString() method.

public enum Type {

    ACCOUNT,
    CLIENT;

    public static Type fromString(final String name) {

        final Type element = sMap.get(name);
        if (element != null) {

            return element;
        }
        throw new IllegalArgumentException(String.format("Unknown value: <%s> for %s", name, Type.class.toString()));
    }

    private static final Map<String, Type> sMap = new HashMap<>();

    static {
        for (final Type t : Type.values()) {

            sMap.put(t.toString(), t);
        }
    }
}

For a more general solution, please checkout http://bytefreaks.net/programming-2/java/java-create-a-lookup-table-for-an-enum, that post will show you how to create a lookup table for any type of value (in contrast to using the enum.toString() method)