mardi 4 août 2015

need help for improving my snake game in C

Its not a programming help

There is a gap coming in the snake body while moving top and bottom , I tried my part and found that its due to console fixed character width in Microsoft OS . can we overcome this in any way ... Please don't say "go for GUI".

Here's the video link



via Chebli Mohamed

Threads creation in C

void print_hello_world() {
    pid_t pid = getpid();
    printf("Hello world %d\n", pid);
    pthread_exit(0);
}

void main() {
    pthread_t thread;
    pthread_create(&thread, NULL, (void *) &print_hello_world, NULL);
    print_hello_world();
}

I really couldn't understand what is the need of (void *) in pthread_create. And do we need "&print_hello_world" in the same or could drop "&" as I have read somewhere that while passing function pointers we don't need to place "&" before the function name.



via Chebli Mohamed

How to convert c program to fully portable for ios

I wrote a simple c program for connect to ldap server using opelLdap c library. now I want to run that c program on ios device. but when I move that c program to xcode project it says ldap.h is missing. ldap.h file is saved in standard include file location so it include this way #include <ldap.h> but I move this file to my xcode project and include it this way #include "ldap.h" it generate so many error because this ldap.h header file contain lot of other standard header files and they have their own dependencies and so on. they all are include this way #include <header.h> it is not possible to convert all the <> to " " one by one. is there any way to to this thing. actually I need to move my code with it's all dependencies I am new to both of this c and xcode(swift/objective-c)



via Chebli Mohamed

Xlib difference between _NET_ACTIVE_WINDOW and XGetInputFocus

What's the difference between _NET_ACTIVE_WINDOW and XGetInputFocus() ? Do they always point to the same window? When use one over the other?

Thanks.



via Chebli Mohamed

C Programming - fprintf and printf in while cicle doesn't work

I'm getting a strange problem with a while cicle inside of a function.

I have to look for the extreme vertices of a .ply model. All the data is stored in a linked list. When I'm done creating the list, I call the findExtremeVertex function, that modifies 6 global variables (leftVertex, rightVertex, downwardVertex, upwardVertex, backVertex and frontVertex).

To see if the values are right (the models I use are a bit too big to control every single line to find the maximum of every vertex) I decided to print every change in the max-min values but, when I try to print them in a file, the file is empty. Why is that? Also, when I saw that the file was empty, I tried to print something directly in the console but that didn't work either.

Here's the code of the funcion:

void findExtremeVertex(Vertex *vertex){
    FILE *modelInfoFile;
    int i = 0;

    ///Giving data to direction-vertices pointers
    leftVertex = malloc(sizeof(Vertex));
    rightVertex = malloc(sizeof(Vertex));
    upwardVertex = malloc(sizeof(Vertex));
    downwardVertex = malloc(sizeof(Vertex));
    frontVertex = malloc(sizeof(Vertex));
    backVertex = malloc(sizeof(Vertex));

    ///Giving the direction-vertices the values of the parameter
    leftVertex = vertex;
    rightVertex = vertex;
    upwardVertex = vertex;
    downwardVertex = vertex;
    frontVertex = vertex;
    backVertex = vertex;

    ///Opening file
    modelInfoFile = fopen(us2, "w");
    if(modelInfoFile == NULL){
        printf("Error in file opening. Exiting.");
        exit(EXIT_FAILURE);
    }

    ///Scrolling the list
    while(vertex->prev != NULL){
        vertex = vertex->prev;

        ///If the given element of the list is more to the right than the global variable,
        ///I assign the values of the element to the global variable
        if(vertex->vertexCoordinates.x > rightVertex->vertexCoordinates.x){
            rightVertex = vertex;
        }

        /**
            I'm omitting the other if constructs because are basically
            the same, but the syntax is correct
        **/

        ///Printing in file the cycle information
        fprintf(modelInfoFile, "********** CYCLE %d **********\n\n", i);
        fprintf(modelInfoFile, "Vertex sx\n");
        fprintf(modelInfoFile, "%1.4f %1.4f %1.4f %1.4f %1.4f %1.4f\n\n", leftVertex->vertexCoordinates.x,
                                                                      leftVertex->vertexCoordinates.y,
                                                                      leftVertex->vertexCoordinates.z,
                                                                      leftVertex->vertexNormals.x,
                                                                      leftVertex->vertexNormals.y,
                                                                      leftVertex->vertexNormals.z);

        /**
            Again, I'm omitting some repetitions but the syntax is correct
        **/
        }
    }

I call this function in another function, but there's no segmentation fault signal, the compiler doesn't tell me anything, the program doesn't crash. I have no clue of the error, except from the fact that the file where I print the infos about the cycles is empty. What am I doing wrong?



via Chebli Mohamed

I'm copying printf code from word, but the quotation marks don't work

So I'm currently doing a reasonably big program, and I need to add in some images. I decided to do this by way of ascii art, so I converted my image, stuck it in word, and using aragrah points and find and replace I stuck 'printf("' at the start of each line, and '\n)"' at the end.

The problem I'm facing is that the " don't work, and considering I've got literally thousands to do before my program has the amount of art it needs, I could really use some help on how to fix them without manually changing them.



via Chebli Mohamed

Tiny Linked List error in C

I have this four files,

tinyll.c tinyll.h in /home/user/lib

test.c tinyll.h in /home/user/code

and compile with this instructions for create a static library libtinyll.a and use it.

; in lib
$ gcc -c tinyll.c
$ ar -cvq libtinyll.a *.o

; in code
$ gcc -o test test.c ../lib/libtinyll.a

Until here all is ok. But I don't know why I obtain segmentation fault because the lines from [CODE ERROR] but showElements work. The target is not pass code from test.c to tinyll.c for treat the tiny list linked. How fix that?

/////////////////////////////////// test.c

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

#include "tinyll.h"

int main(int argc, char *argv[])
{    
    progname = argv[0];

    char *file = "fwords";
    int n;
    PTLL lsone = NULL;
    n = loadfileinTLL(file,&lsone);

    // work. good. 
    showElements(lsone);

    // [CODE ERROR]
    // Why here dont work?
    // segmentation fault, load the first word
    // but in the second crash.
    while (lsone != NULL) {
        printf("%s",lsone->word);
        lsone = lsone->next;
    }    
    return 0;
}

/////////////////////////////////// tinyll.c

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

#include "tinyll.h"

void addElement(PPTLL lst, char data[])
{
    PTLL elemt;
    elemt = (PTLL) malloc(sizeof(TLL));

    if (elemt == NULL) {
        fprintf(stderr, "%s: insufficient memory.\n", progname);
        exit(1);    
    }

    if (*lst == NULL)
    {
        strncpy(elemt->word, data, 45);
        elemt->next = NULL;
        *lst = elemt;
    }
    else {
        // add in front of list
        strncpy(elemt->word, data, 45);
        elemt->next = *lst;
        *lst = elemt;
    }
}

void showElements(PTLL lst)
{
    while (lst != NULL) {
        printf("%s\n",lst->word);
        lst = lst->next;
    }
}

int loadfileinTLL(char *filepath, PPTLL plst) 
{
    FILE *f;
    if ((f = fopen(filepath, "r")) == NULL) {
        fprintf(stderr, "%s: error to load file %s.\n", progname, filepath);
        exit(1);
    }

    char buf[45]; int n=0;
    while (fgets(buf,sizeof(buf),f) != NULL) {
        char *nl;
        if ((nl = strchr(buf,'\n')) != NULL) {
            *nl = '\0';
        }
        addElement(plst,buf);
        n++;
    }

    fclose(f);

    return n;
}

//////////////////////////////////// tinyll.h

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

#ifndef _TINYLL_
#define _TINYLL_

struct list {
    char word[45];
    struct list *next;
};

// Tiny Linked List
typedef struct list TLL;
typedef struct list *PTLL;
typedef struct list **PPTLL;
char *progname;

#endif



via Chebli Mohamed