irre 17 Newbie Poster

you need it for dynamic memory allocation. just think of a program where sb has to input a n-dimensional matrix of unkown n. let the user input n and then go forward to get the necessary memory space.
next advantage of malloc is that all the memory-places allocated stick next to each other. if you would set up static variables, their locations would be at random places. for example you want to allocate a dynamic nxn-matrix:

double *matrix;
int n;
printf("input dimension:");
scanf("%d",&n);
matrix=(double*)malloc(n*sizeof(double));

malloc is a void function so you have to bring it into a any pointer type like (int*),(float*),or(double*)
matrix then represents the start address of your memoryspace allocated.
You can access these spaces like following:

scanf("%lf",matrix[i]);  // i=0,1,2...,n-1
//that should be the same as
scanf("%lf",matrix)          //for i=0
//or
scanf("%d",matrix+3);        //for i=3

you wont need the address operator "&" cause pointers are adresses.

irre 17 Newbie Poster

--var computes the condition first and updates the variable afterwards

var-- updates the variable first and uses the updated value of var to compute the condition

the other way round

irre 17 Newbie Poster

Thanks for the replies, but could you show me what you mean?

Thanks again.

if you code something like

int i;
do
{
    i++;
}
while(1);

it will jump into the do/while loop and start incrementing i (i++; -> i=i+1; ). after that it will look if the statment given in brackets is true or false. in c a true statement is 1 and a false one is 0. if the statement is true the loop will be looped once again, after that the statement is checked again.
you also could code while(i>0)... this also would be true for a start i=0;

irre 17 Newbie Poster

i usually use do{...}while(1);
1 is always true, therefore it will stay in the loop. if you want to get out of it, the command "break;" should do it.

irre 17 Newbie Poster

Nice try, but it only calculates the days for one month. What it needs is the number of days from 1 Jan to the beginning of the specified month.

wrong. it calculates all days of the year up to the current date. as you can see the switch cases do not get breaked, therefore it sums up all previous months.

Ancient Dragon commented: right :) +17
irre 17 Newbie Poster

actually you dont need a function for leap year detection...

just take a variable and put it 1 or 0 in the if/else if/else part in case 2

int whatever;
               case 2:if((!(y%4))&&(y%100)){
                   days+=29;
                   whatever=1;
                   }
               else if(!(y%400)){
                   days+=29;
                   whatever=1;
                   }
               else{
                   days+=28;
                   whatever=0;
                   }

sorry for double posting, i wasnt able to edit my previous post again

irre 17 Newbie Poster
#include <stdio.h>

void main()
{
    int d,m,y;
    int days=0;
    int k;                            // return var
    printf("input date dd.mm.yyyy\n");
    scanf("%d.%d.%d",&d,&m,&y);
    m--;
    switch(m)
    {
        case 11:days=30;
        case 10:days+=31;
        case 9:days+=30;
        case 8:days+=31;
        case 7:days+=31;
        case 6:days+=30;
        case 5:days+=31;
        case 4:days+=30;
        case 3:days+=31;
        case 2:if((!(y%4))&&(y%100))
                   days+=29;
               else if(!(y%400))
                   days+=29;
               else
                   days+=28;
        case 1:days+=31;
        case 0:break;
    }
    k=leap(y);
    printf("days:%d\nleap year:%d\n\n",days+d,k);
}

int leap(int year)
{
    int k;
    if((!(year%4))&&(year%100))
        k=1;
    else if(!(year%400))
        k=1;
    else
        k=0;
    
    return k;
}
irre 17 Newbie Poster

hi there

im wondering about the difference between:

int nr;
if(!((nr--)%5)){...}
if(!((nr--)%5)){...}

and:

int nr;
if(!(--nr)%5)){...}
if(!(--nr)%5)){...}

is it right that in case 1 nr will be decremented after true/false comparison and in case 2 in advance?

assuming nr=123456;
in case 1 it would be true for second if.... and
in case 2 it would be true for first if?


im not too sure about the difference between --var and var--

irre 17 Newbie Poster

using #include <cnum.h> does not help... as far as i know do the quotation marks only mean that the compiler looks in the source file folder for the library in addition, and not only in that folder.

i askes the writer of that library whether there is a .lib file that i can use, i dont see any other solution

or is there a way to create a .lib file out of a .a file?

irre 17 Newbie Poster

hello,

im totally new to this forum, but i already checked whether my ask is answered somewhere else already. i couldnt find anything...

i used to work with dev-cpp and i have to work with the cnum.h library a lot. Since im a student i am able to get MS Visual Studio 2010 for free. after realising that, i installed it.

But there is my problem:
I want to include cnum.h file into my clean cpp project. in order to do so, i added the folder of the cnum.h and the libCNum.a file to the linker (additional library directories, im not too sure if this is the correct english name for it, my system is running in german language)

after this i included the .h file with #include "cnum.h"
and i typed the code where i use one function out of this library.

when i try to compile i get following message:
": fatal error C1083: Datei (Include) kann nicht geöffnet werden: "cnum.h": No such file or directory"

("Datei (Include) kann nicht geöffnet werden" means "not able to open include file")

Is there a special folder where i have to paste the cnum.h and the libcnum.a files?


hope you can help me