Sokurenko 42 Junior Poster

about *ptr-arr here is the debug code. Hope you figure this out need to sleep now. Sorry for no comments i can do it later if there are questions.

#include<stdio.h>

int main()
{
        static int arr[]={0,1,2,3,4};
        int *p[]={arr,arr+1,arr+2,arr+3,arr+4};
        int **ptr=p;
        printf("size of int %d\n", sizeof(int));
        printf("ptr before changes %d\n", ptr);
        printf("p before changes %d\n", p);
        printf("arr before changes %d\n", arr);
        printf("*ptr before changes(points to arr) %d\n", *ptr);
        ptr++;
        printf("ptr after increment %d\n", ptr);
        printf("*ptr after increment(arr+1) %d\n", *ptr);
        printf("\n How many separate ptr and p [%d] %d %d \n",ptr-p,*ptr-arr,**ptr);
        *ptr++;
        printf("\n %d %d %d \n",ptr-p,*ptr-arr,**ptr);
        *++ptr;
        printf("\n %d %d %d \n",ptr-p,*ptr-arr,**ptr);
        ++*ptr;
        printf("\n %d %d %d \n",ptr-p,*ptr-arr,**ptr);
        return 0;
}

http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence
http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/BitOp/pointer.html

Dont be afraid to add as much printf as possible.

Sokurenko 42 Junior Poster

When you subtract two pointers, as long as they point into the same array, the result is the number of elements separating them and in this case each int is 4 byte for example.. then you get this otput on my pc:
size of int 4
ptr before changes 2293496
p before changes 2293496
ptr after increment 2293500

How many separate ptr and p [1] 1 1

2 2 2

3 3 3

3 4 4

Process returned 0 (0x0) execution time : 0.029 s
Press any key to continue.

#include<stdio.h>

int main()
{
        static int arr[]={0,1,2,3,4};
        int *p[]={arr,arr+1,arr+2,arr+3,arr+4};
        int **ptr=p;
        printf("size of int %d\n", sizeof(int));
        printf("ptr before changes %d\n", ptr);
        printf("p before changes %d\n", p);
        ptr++;
        printf("ptr after increment %d\n", ptr);
        printf("\n How many separate ptr and p [%d] %d %d \n",ptr-p,*ptr-arr,**ptr);
        *ptr++;
        printf("\n %d %d %d \n",ptr-p,*ptr-arr,**ptr);
        *++ptr;
        printf("\n %d %d %d \n",ptr-p,*ptr-arr,**ptr);
        ++*ptr;
        printf("\n %d %d %d \n",ptr-p,*ptr-arr,**ptr);
        return 0;
}
Sokurenko 42 Junior Poster

Try otputing to another file instead of windows console and read, as i see you use dos include.
Please note that windows new line is not '\n' because it's one byte in linux LF and 2 byte in windows CR+LF please see http://en.wikipedia.org/wiki/Newline

Sokurenko 42 Junior Poster

please try different compiler maybe code blocks IDE or other

Sokurenko 42 Junior Poster

paste this into your file manager to test
c:\TurboC++\Disk\TurboC3\sample.txt

or use sameple.txt
and put it in same dir as your exe

Sokurenko 42 Junior Poster

looks like i flipped bmp image with this code, bot it changed color ))
also still meta data unknown
http://www.cs.pitt.edu/~kirk/cs1501/Ramirez/Spring2008/assigs/compression/data/tux-large.bmp

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


int iCreateTestFile();
int iReverseImage(unsigned char *pucImageData, long lSize)
{
   FILE * pfOutputImage = NULL;
   long i = 0;
   long j = 0;
   unsigned char *pucReversedImage = (unsigned char*) malloc(lSize);

   i = lSize - 1;
   j = 100;
   while(j < lSize)
   {
      pucReversedImage[i] = pucImageData[j];
      j++;
      i--;
   }

   pfOutputImage = fopen("tux-large-reversed.bmp", "wb");
   fseek (pfOutputImage , 0 , SEEK_SET);
   fwrite(pucImageData, 1 , 100 , pfOutputImage);
   fseek (pfOutputImage , 100 , SEEK_SET);
   fwrite(pucReversedImage, 1 , lSize-100, pfOutputImage);
   fclose(pfOutputImage);
   free(pucReversedImage);
   return 0;
}


int main()
{  
   iCreateTestFile();

   return 0;
}

int iCreateTestFile()
{
   FILE *pfInputImage = NULL;
   long lSize = 0;
   unsigned char *pucImageData = NULL;

   pfInputImage = fopen("tux-large.bmp", "rb");
   fseek (pfInputImage , 0 , SEEK_END);
   lSize = ftell (pfInputImage);
   fseek (pfInputImage , 0 , SEEK_SET);
   pucImageData = (unsigned char*) malloc(lSize);
   fread(pucImageData, 1 , lSize, pfInputImage);

   if(pucImageData != NULL)
   {
      iReverseImage(pucImageData, lSize);
   }

   fclose(pfInputImage);
   free(pucImageData);
   return 0;
}
Sokurenko 42 Junior Poster

Maybe someone can help me understand how can i read metadata of the jpeg image on c as i understand there are some tags ?
Also is this data 2 byte not one byte to reverse >\?
I am using linux so maybe some library ?
Here is the code and i need to reverse image bot first determine metadata size not to reverse it..

Here is some code for image http://www.scs.ryerson.ca/~etabello/linux_icon.jpg

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


int iCreateTestFile();
int iReverseImage(unsigned char *pucImageData, long lSize)
{
   FILE * pfOutputImage = NULL;
   long i = 0;
   long j = 0;
   unsigned char *pucReversedImage = (unsigned char*) malloc(lSize);

   i = lSize - 1;
   j = 0;
   while(j < lSize)
   {
      pucReversedImage[i] = pucImageData[j];
      j++;
      i--;
   }

   pfOutputImage = fopen("linux_icon_reversed.jpg", "wb");

   fwrite(pucReversedImage, 1 , lSize, pfOutputImage);
   fclose(pfOutputImage);
   free(pucReversedImage);
   return 0;
}


int main()
{  
   iCreateTestFile();

   return 0;
}

int iCreateTestFile()
{
   FILE *pfInputImage = NULL;
   long lSize = 0;
   unsigned char *pucImageData = NULL;

   pfInputImage = fopen("linux_icon.jpg", "rb");
   fseek (pfInputImage , 0 , SEEK_END);
   lSize = ftell (pfInputImage);
   fseek (pfInputImage , 0 , SEEK_SET);
   pucImageData = (unsigned char*) malloc(lSize);
   fread(pucImageData, 1 , lSize, pfInputImage);

   if(pucImageData != NULL)
   {
      iReverseImage(pucImageData, lSize);
   }

   fclose(pfInputImage);
   free(pucImageData);
   return 0;
}
Sokurenko 42 Junior Poster

are you shure you can use fgets ? why dont you use fread for start and try to copy image ?
You can also use some hex editor to compare values.

fgets is for text...

Sokurenko 42 Junior Poster

No, everyone has them, you need to be patient. Cheer up :)
Just always know that this is c language, there is no magic, if something does not work then you can find out why always. Add as much printf as you can to do that..
Like that

void checkcollision()
{
    float x1 = tanks[0].x;
    float y1 = tanks[0].y;
    float x2 = tanks[1].x;
    float y2 = tanks[1].y;
    float dist = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
    float dr = tanks[0].rad+tanks[1].rad;
    printf("dist: %f, radius+radius: %f\n",dist, dr);
    if(1 < 2)
    {
       puts("yes");
    }
    else
    {
       puts("wtf");
    }
    if(dist < dr) ;
    {
        printf("checkcollision read true\n");
        printf("dist: %f, radius+radius: %f\n",dist,dr);
        goback(0); goback(1);
    }
}

thank you. Am I the only one who has trouble seeing small problems in walls of text that comprise code? This is depressing.

Sokurenko 42 Junior Poster

if(dist<dr);
remove semicolon ?
should be or it will do nothing if dist < dr and do nothing on else

    if(dist<dr)
    {
        printf("checkcollision read true\n");
        printf("dist: %f, radius+radius: %f\n",dist,dr);
        goback(0); goback(1);
    }
Sokurenko 42 Junior Poster

did you try binary mode ?
http://www.cplusplus.com/reference/clibrary/cstdio/fread/
it should work very goot if you work with bytes.
for example - read whole file to buffer then run through all bytes and if byte is not 0 output to result buffer

Sokurenko 42 Junior Poster

It looks like it works, please explain better or post file.
Also, yes you should take hex editor to know exactly wat you are dealing with.. If it is not 8bit chars then you need to parse it differently..
check out dragon post
http://www.daniweb.com/software-development/c/threads/254246/how-to-read-unicode-utf-8-binary-file-line-by-line

Sokurenko 42 Junior Poster

the problem is that your code is impossible to copy cause of line numbers.
try to use malloc

#include "stdio.h"
#include "stdlib.h"
 void *gfunc_Realloc (void *i_oldObject_p, unsigned long i_objectSize)
 {
 /* Local variable *\
 \* -------------- */

 void *newObject_p;

 /* Code *\
 \* ---- */

 if(NULL == i_oldObject_p) {
 newObject_p = malloc(i_objectSize);
 } else {
 if (NULL == (newObject_p =
 realloc (i_oldObject_p,i_objectSize))) {
 printf("memory reallocation error\n");
 exit(1);
 }
 }
 return newObject_p;
}

int main()
{
   char *pcBuffer = NULL;
   while(1)
   gfunc_Realloc(pcBuffer, 500);
   return 0;
}
Sokurenko 42 Junior Poster

you reference wrong address, this causes core dump yes ?
you add fprintf and maybe initialise some memmory then reference it by mistake, this could be answer m ? check address you reference, and addresses you allocate with memmory

Sokurenko 42 Junior Poster

this for date ? :) and it should be very portable
Click Here

Sokurenko 42 Junior Poster

you need to modify only pointer "next", do you know what linked list is? If you know then you can draw something that you whant to do with it and then program this behaviour

Sokurenko 42 Junior Poster

can you try this ? or strtol, please say if works

while(fscanf(f,"%d",&value) == 1)
{
/* do something with the value */
}
Sokurenko 42 Junior Poster

you are not the first nor the last who needs to read from file, search google, good luck to you :)

http://www.cplusplus.com/reference/clibrary/cstdio/fopen/

Sokurenko 42 Junior Poster

maybe you can use this
http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/

#include <stdlib.h>

int main()
{
   char *pEnd;
   printf("%ld\n",strtol("A",&pEnd,16));
   return 0;
}
Sokurenko 42 Junior Poster
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HOW_MANY_STRUCTS 20
#define MAX_NAME_LENGTH 30
typedef struct ritnish
{
    int iRoll;
    char pcName[MAX_NAME_LENGTH];
}stRitnish;

int main()
{
   stRitnish s[HOW_MANY_STRUCTS] = {0};
   srand(time(NULL));
   memcpy(s[0].pcName, "TestName!",sizeof("TestName!"));
   s[0].iRoll = rand() % 1000;
   printf("Printing name [%s] and number %d\n", s[0].pcName, s[0].iRoll);

   return 0;
}

ok waltp ill do that what is difference between using void main() and int main() because void means it returns empty .but int returns 0

what is empty ?
there is no magic, value is value i suppose

Sokurenko 42 Junior Poster

this i wrote for linux what is your platform ? my ubuntu - gcc

#include <time.h>
#include <stdio.h>

int main()
{
   time_t startSeconds;

   startSeconds = time(NULL);

   while(1)
   {
      system("clear");//platform specific check your platform this is linux/unix
      printf("%ld \n", time(NULL) - startSeconds);
      sleep(1);//platform specific check your platform and change this is linux/unix
   }
   return 0;
}
Sokurenko 42 Junior Poster

post some example of log file, and what do you expect of it

Sokurenko 42 Junior Poster

http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
Return Value
On success, the function returns the number of items successfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens.
In the case of an input failure before any data could be successfully read, EOF is returned.

Sokurenko 42 Junior Poster

this is what it means fifth is null terminator 065 in oct is '5' check ascii table

#include <stdio.h>

int main()
{
   char str[]="S\065AB";
   char strSame[5] = { 'S','\065', 'A', 'B', '\0'};

   printf("str 1 %d \n", sizeof(str));
   printf("str 2 %d\n", sizeof(strSame));
   printf("same ? %d [%s] [%s]",strcmp(str, strSame),str, strSame);   

   return 0;
}
Ancient Dragon commented: nice :) +14
Sokurenko 42 Junior Poster
Sokurenko 42 Junior Poster

float is 8 bytes and int 4 bytes if you say %f you expect 8 bytes and type float

#include <stdio.h>

int main()
{
   int iInt = 1;

   printf("int %d float %f",iInt, (float)iInt);
   return 0;
}

btw i suggest you read char buffer in and then convert it to float or int

Sokurenko 42 Junior Poster
Sokurenko 42 Junior Poster

or something like that

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

char *safe_gets(char *buf, size_t size)
{
    if (!fgets(buf, size, stdin))
        return NULL;

    buf[strcspn(buf, "\n")] = '\0';

    return buf;
}

int main(void)
{
    char str[7] = {0};
    int i, k = 0;
    int iLen = 0;
    printf("Enter string 1: ");
    fflush(stdout);

    if (!safe_gets(str, sizeof str)) {
        fprintf(stderr, "Input error\n");
        return EXIT_FAILURE;
    }

    printf("Enter string 2: ");
    fflush(stdout);
    iLen = strlen(str);
    if (!safe_gets(&str[iLen], sizeof str - iLen)) {
        fprintf(stderr, "Input error\n");
        return EXIT_FAILURE;
    }

    printf("\n%s\n", str);

    return EXIT_SUCCESS;
}
Sokurenko 42 Junior Poster

take a look at this Click Here

Sokurenko 42 Junior Poster
int a[]={1,2,3,4,5,6,7,8,9,10};

You allocate memmory on stack here and a points to that memmory.
Then pointer b points to that memmory location b=a
i mean now b and a point to same location so you can work with it same as with a

here you can try to print it's addresses to check :)

#include <stdio.h>

int main()
{
   char pcArray[3] = {0};
   char *pcToArray = pcArray;
   printf("pcArray == %x and pcToArray == %x",pcArray,pcToArray);
   return 0;
}
Sokurenko 42 Junior Poster

here is good link i found on google Click Here

Sokurenko 42 Junior Poster

use

printf("size of char %d size of int %d",sizeof(char),sizeof(int));

bot pointer is always 4 byte no ?

maybe you can try to cast to char ?

by the way where is problem?

strcpy(*rowptr,"d");
    strcpy(*(rowptr+1),"c++");
    printf("ROWPTR+1[%s]",*(rowptr + 1));
    strcpy(*(rowptr+2),"java");
    printf("ROWPTR+2[%s]",*(rowptr + 2));

they print correct, no ?

please see howto allocate memmory for pointer to pointer Click Here took code from there

float **float_values;
//allocate memory for rows
float_values = (float**)malloc(4 *sizeof(float*));
//for each row allocate memory for columns
for(int i=0; i<4; i++)
{
   *(float_values+i) = (float*)malloc(3 *sizeof(float));
}
Sokurenko 42 Junior Poster

Indeed if you like programming then just sit and do it, there is nothing impossible, you just need time.

Sokurenko 42 Junior Poster

Actually all you need is to call function bigger 3 times in printbiggest.
If input parameters x i z j bot you will need extra parameter inside function iMax
first you get to know x and y biggest then save it and compare it to z then save it and compare it to j ?

We may help you if you post something it's small program one line in biggest with ternary expre.. and maybe 5 in print biggest

Sokurenko 42 Junior Poster

your head is not null ?

Sokurenko 42 Junior Poster

Is it necessary to free the memory of the node that is deleted?

What do you mean deleted then ? In c if you loose pointer to some data it is called memmory leak, cause no automatic garbage collection is implemented in c. In java if all references lost then free is called, you should do it manually before loosing this pointer. For example save address of some data you whant to free to tmp variable then make previous element to point to next after tmp after your linked list is ready (does not cointain this element anymore) you let go - free(tmp). Also should check if it is first and stuff..

and yes you can return this tmp pointer bot then you haveto rename this function to cutByPosition and then use this pointer to insert somewhere else.

please whole code

Sokurenko 42 Junior Poster

use this Click Here

ifeq ($(BUILD),debug)   
# "Debug" build - no optimization, and debugging symbols
CFLAGS += -O0 -g
else
# "Release" build - optimization, and no debug symbols
CFLAGS += -O2 -s -DNDEBUG
endif

all: foo

debug:
    make "BUILD=debug"

foo: foo.o
    # The rest of the makefile comes here...
Sokurenko 42 Junior Poster

How do you reverse a linked list without using any C pointers?

One way is to reverse the data in the nodes without changing the pointers themselves. One can also create a new linked list which is the reverse of the original linked list. A simple C program can do that for you. Please note that you would still use the "next" pointer fields to traverse through the linked list (So in effect, you are using the pointers, but you are not changing them when reversing the linked list).
Source

Sokurenko 42 Junior Poster

this means you have something like that ? char *array[n];
you have allocated pointers bot now you need to malloc memmory where they will point and memcpy data to that memmory, how do you copy it i dont see

Sokurenko 42 Junior Poster

How do you reverse a linked list without using any C pointers?

Reverse data structure that is linked by pointers without using pointers ? Really ? :)) No cant.

Sokurenko 42 Junior Poster

should it work same as strtok ? try not to use malloc and use array index instead of pointer arithmetic then when it works you can try pointer arithmetic(just increment address instead of index) and malloc
Make working code and convert would be more easy maybe :)

Sokurenko 42 Junior Poster

@Trentacle
so if i use stack as singly linked list i dont travel reverse when i pop it ? even link Click Here

Sokurenko 42 Junior Poster

use this wiki page that i gave you there is stack implementation algorithm, then post code and someone will help you

Sokurenko 42 Junior Poster

there is no problem to do backward or frontward in single linked, as long as it is only one way to go needed, is more needed is both or one ? or you just go from start agayn to that spot you need. Or depends how much back you whant to go if it is by one to delete then you delete element-next not element and you good to go

Sokurenko 42 Junior Poster

do you know how to create linked list ? if yes then to reverse you can use this post Click Here you just take your list and while you traverse it just move to new in reversed order

Sokurenko 42 Junior Poster

Maybe it is stack ? you push new to first and then pop from first till last ? :) that will be reverse

Sokurenko 42 Junior Poster

here is the guy who added code to help other Click Here
this is related article maybe, dont know your level and what you whant really though. Can you describe what you whant to do ? What your program must do

also this wiki it even has algorithm :)

Sokurenko 42 Junior Poster

use g++ on linux if those ide are too hard to understand with g++ you will have full control

Sokurenko 42 Junior Poster

he uses bubble sort oh no so does it work ? what will happen if he try to sort sorted ? worst case scenario yes ? need some flag atleast if nothing sorted in second cycle then no need to continue

Sokurenko 42 Junior Poster

Operator precendece
maybe this will help Click Here too