vendredi 14 août 2015

How to change VAR name without getting the undeclared error after 'make install'?

My configure.in file has:

VAR=yo
AC_SUBST(VAR)

Makefile.am has:

bin_PROGRAMS = hello
hello_SOURCES = hello.c 
hello_CFLAGS =-DVAR=@VAR@

C file is:

#include <stdio.h>
int main()
{
    printf("%s\n",VAR);
    return 0;
}

When I do 'make install' there is error

Making install in src
make[1]: Entering directory `/home/albertpod/hello/src'
if gcc -DPACKAGE_NAME=\"hello\" -DPACKAGE_TARNAME=\"hello\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"hello\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"hello\" -DVERSION=\"1.0\" -I. -I.    -DVAR=yo -g -O2 -MT hello-hello.o -MD -MP -MF ".deps/hello-hello.Tpo" -c -o hello-hello.o `test -f 'hello.c' || echo './'`hello.c; \
    then mv -f ".deps/hello-hello.Tpo" ".deps/hello-hello.Po"; else rm -f ".deps/hello-hello.Tpo"; exit 1; fi
hello.c: In function ‘main’:
hello.c:8:13: error: ‘yo’ undeclared (first use in this function)
hello.c:8:13: note: each undeclared identifier is reported only once for each function it appears in
make[1]: *** [hello-hello.o] Error 1
make[1]: Leaving directory `/home/albertpod/hello/src'
make: *** [install-recursive] Error 1

So name of VAR becomes yo, but it is undeclared. My goal is to print yo, but how to solve this case?



via Chebli Mohamed

Balanced Expression

Below is the code to determine balancing of symbol.

If the expression is balanced then print appropriate message

E.g. ((A+B))+(C+D)) --> Balanced

((A+B)+(C+D) ---> Unbalanced

((A+B)+(C+D}) --> Unbalanced

Here is the code

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

struct Stack{
char data;
struct Stack *next;
};

void push(struct Stack **top,char data)
{
        struct Stack *new_node;
        if(*top==NULL)
        {
                new_node=malloc(sizeof(struct Stack));
                new_node->data=data;
                new_node->next=*top;
                *top=new_node;
        }
        else
        {
                new_node=malloc(sizeof(struct Stack));
                new_node->data=data;
                new_node->next=*top;
                *top=new_node;
        }
}
char pop(struct Stack **top,int flag)
{
        if(*top!=NULL && flag==0)
        {
                printf("\n Expression is In-Valid  :) \n");
                return '\0';
        }
        if(*top==NULL && flag ==1)
        {
                printf("\n Unbalanced Expression \n");
                return '\0';
        }
        if(*top!=NULL && flag==1)
        {
                struct Stack *temp=*top;
                char op;
                op=(*top)->data;
                *top=(*top)->next;
                free(temp);
                return op;
        }
}
/*void display(struct Stack *top)
{
        struct Stack *temp=top;
        while(temp)
        {
                printf("\n %c",temp->data);
                temp=temp->next;
        }
} */
int main(void)
{
        struct Stack *top=NULL;
        int i=0;
        char str[]="((A+B)+[C+D])",op;
        printf("\n Running the programe to check if the string is balanced or not ");
        for(i=0;str[i]!='\0';++i)
        {
                if(str[i]=='('||str[i]=='['||str[i]=='{'||str[i]=='<')
                        push(&top,str[i]);
                else if(str[i]==')'||str[i]==']'||str[i]=='}'||str[i]=='>')
                {
                        op=pop(&top,1);
                        if( (op=='('&&str[i]==')') || (op=='['&&str[i]==']') || (op=='{'&&str[i]=='}') || (op=='<'&&str[i]=='>'))
                        {
                                continue;
                        }
                        else
                        {
                                printf("\n The expression is un-balanced \n");
                                break;
                        }

                }

        }
        pop(&top,0);
        return 0;
}

But it does not give the desirable output. I have debuged the code but not able to find the issue

Can u please help and look into it ?



via Chebli Mohamed

How to use abs and div with fixed-size integers

In C99 we have fixed-size integer types, defined in stdint.h. In stdlib.h we have the abs and div functions which operate on ints, as well as their long int/long long int counterparts labs, llabs, ldiv, lldiv.

Since the size of int/long/long long varies based on platform and used compiler I wonder how to chose the right variant of abs/div when using fixed-size integers like int16_t, int32_t or int64_t?



via Chebli Mohamed

How to concatenate a name of file using fopen function?

I want to create a file which I create this code:

void create_file(char name[],char user[],char date[]){

FILE* fichier=NULL;
fichier=fopen(strcat(name,".txt"),"a+");
fprintf(fichier,"user: %s connected at : %s \n",user,date);
fclose(fichier);

}

But it doesn't work.



via Chebli Mohamed

[solved]prevent strings overflowing in c

Using C I want to allocate heap space of the maximum initial string length which is 6 in my case and then each time the string is about to overflow, double the available space. I want to build the string char by char.

size_t nstring = 6;
char *arr =  malloc(nstring);

int i;
for (i = 0; i < 55;i++){

        if (i == nstring) {
                printf("mAx sized!!");
                arr = realloc(arr, nstring*2);
                nstring = nstring*2;
            }
    arr[i] = 'f';

}
arr[i] = '\0';
printf("length %lu\n", strlen(arr));
free(arr);



via Chebli Mohamed

Re-allocating array of structure with realloc?

I'm practicing on structures, dynamic memory and file I/O but I can't understand what is wrong with this code. I suspect that the error is with realloc function. When I run the program after the file opening, the program crash. I check a lot of times and I suppose that is not a syntax related problem(especially with realloc).

Structure declaration:

#define MAXLENPATH 250
#define MAXSTRING 25

typedef struct
{
    int ID;
    char Name[MAXSTRING];
    char Surname[MAXSTRING];
    char code[MAXSTRING];
    int age;
}person;

Function to get record info:

#include <stdio.h>
#include <stdlib.h>
#include "Data.h"
#include "DataBase.h"

void initRecord(person *Person)
{
    printf("Insert ID:\n");
    scanf("%d", &Person->ID);

    printf("Insert Name:\n");
    scanf("%s", Person->Name);

    printf("Insert Surname:\n");
    scanf("%s", Person->Surname);

    printf("Insert Age:\n");
    scanf("%d", &Person->age);

    printf("Insert code:\n");
    scanf("%s", Person->code);

}

Main function:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "Data.h"
#include "DataBase.h"

int main(void) {

    FILE *ts;
    char *fpath=NULL;
    int pCount=1; //variable to hold database records count
    int i=0;
    int toAdd=0;
    person *personDB=NULL;

    fpath="C:\\Users\\Pio\\Desktop\\FILEOP\\MYTXT.txt";

    personDB = malloc(pCount*sizeof(person));

    //fill the database(in memory)
    i=0;
    while( i < pCount)
    {
        printf("Record %d\n",i+1);
        initRecord(&personDB[i]); //fill i-th database record
        printf("Record %d ID:%d Name:%s Surname:%s Age:%d Code:%s \n", i+1, personDB[i].ID, personDB[i].Name, personDB[i].Surname, personDB[i].age, personDB[i].code );
        i++;


        if(i == pCount) // check if all records are filled, then ask to user how many records wants and realloc new memory
        {
            printf("Insert element to add to database records:");
            scanf("%d", &toAdd);
            personDB=realloc(personDB, toAdd*(sizeof(person)) );
            pCount += toAdd;
        }

    }

    //Print structure on file
    if ((ts=fopen(fpath, "w")) != NULL)
    {
        puts("FILE OPENED");
        fprintf(ts,"************INFO****************\n");
        i=0;
        while (i < pCount)
        {

            fprintf(ts, "Record %d ID:%d Name:%s Surname:%s Age:%d Code:%s \n", i+1, personDB[i].ID, personDB[i].Name, personDB[i].Surname, personDB[i].age, personDB[i].code );

            i++;

        }
        fclose(ts);
    }
    else
        perror("ERROR:");

    system("pause");
    return EXIT_SUCCESS;
}



via Chebli Mohamed

Can I do data buffering and data plotting at the same time?

I am using Quartus 12.1 sp1, vhdl and Altera Nios II programmed in C code for DE0-Nano Development. Basically, what I have is, data is sent from fpga vhdl block to NIos II system, Nios II system sends the data to serial port, Matlab access the serial port to real time plot the graph.

I am buffering data received from fpga vhdl say 1000 sample points and save it in sdram... after saving these 1000 sample points, the C code will send data to uart so that Matlab can access these serial port data and plot the graph real time.

Once the sdram has all the first 1000 samples points, processor sends the data to serial port for Matlab to plot the graph, then processor proceeds to save the second set of 1000 samples points, then processor sends the data to serial port for Matlab in order to plot the graph, keep repeating for following sets....etc

In my system, the rate the graph is plotted is much slower than the rate the buffering can be done.

My question is, from the perspective of C programming, when the processor is sending data for plotting, can the processor still do the buffering? If not, what should I do so that when the processor is doing the buffering, I still can get the graph plotted at the same time for the previous set of data?

Appreciate any input, forgive newbie question...thank you



via Chebli Mohamed