Is there the uses of pointer right? - c

I want to use pointer in here, but it's not right.
It has Thread 1: breakpoint 1.1 (1) in
(*(exy + i)) = g;
Please tell me how use the operation of pointer.
#define _CRT_SECURE_NO_WARNINGS
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wstrict-prototypes"
#include<stdio.h>
#include<stdlib.h>
#define M 3
#define N 3
int readmoduluskai(filename, time, gcoef, kcoef, ge, ke, dm)
char filename[];
char dm[];
double *time, *gcoef, *kcoef;
double *ge, *ke;
{
FILE *fp;
int i = 0;
printf("Modulus file name : ");
scanf("%s", filename);
if (NULL == (fp = fopen(filename, "r"))) {
printf("\7\n Cannot open file..\n\n");
exit(1);
}
//fscanf(fp, "%12lg", &ge);
//fscanf(fp, "%12lg", &ke);
//printf("%12lg %12lg \n", ge, ke);
while (3 == fscanf(fp, "%lg %lg %lg", time, gcoef, kcoef)) {
printf("%12lg %12lg %12lg \n", *(time), *(gcoef), *(kcoef));
time++;
gcoef++;
kcoef++;
i++;
}
//printf("Please enter the constitutive parameters : ");
//scanf("%s", dm);
fclose(fp);
return i;
}
int readdata(file, t, ex, ey, ez, eyz, ezx, exy, ekk)
char *file;
double *t, *ex, *ey, *ez, *eyz, *ezx, *exy, *ekk;
{
double a, b, c, g;
FILE *fp;
int i = 0;
printf("File name : ");
scanf("%s", file);
if (NULL == (fp = fopen(file, "r"))) {
printf("Cannot open file..\n\n");
exit(1);
}
while (4 == fscanf(fp, "%lg %lg %lg %lg", &a, &b, &c, &g)) {
printf("%12lf %12lf %12lf %12lf \n", a, b, c, g);
(*(t + i)) = a;
(*(ex + i)) = b;
(*(ey + i)) = c;
(*(exy + i)) = g;
i++;
}
fclose(fp);
return i;
}
int main(int argc, char *argv[])
{
char filename[20];
char dm[2];
char file[20];
double *time;
double ptime = 0;
time = &ptime;
double *gcoef;
double pgcoef = 0;
gcoef = &pgcoef;
double *kcoef;
double pkcoef = 0;
kcoef = &pkcoef;
double *ge;
double pge = 0;
ge = &pge;
double *ke;
double pke = 0;
ke = &pke;
double *t;
double pt = 0;
t = &pt;
double *ex;
double pex = 0;
ex = &pex;
double *ey;
double pey = 0;
ey = &pey;
double *ez;
double pez = 0;
ez = &pez;
double *eyz;
double peyz = 0;
eyz = &peyz;
double *ezx;
double pezx = 0;
ezx = &pezx;
double *exy;
double pexy = 0;
exy = &pexy;
double *ekk;
double pekk = 0;
ekk = &pekk;
time = (double *)malloc(sizeof(double));
gcoef = (double *)malloc(sizeof(double));
kcoef = (double *)malloc(sizeof(double));
ge = (double *)malloc(sizeof(double));
ke = (double *)malloc(sizeof(double));
readmoduluskai(filename, time, gcoef, kcoef, ge, ke, dm);//read the modulus parameters
readdata(file, t, ex, ey, ez, eyz, ezx, exy, ekk);
free(time);
free(gcoef);
free(kcoef);
free(ge);
free(ke);
return 0;
}
how can I change it

Related

Returning the address of a structure

This is the code I am working on and in the function trace* readTrace(char* fileName) I have to read a file (that fills structure) and then return the address of trace structure. Also the time and value of the structure are pointers but I don't know how to do it.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#define TMAX 1000
#define NBPTS 2000
#define DT 0.5
typedef struct
{
char comment[40];
int nbpts;
float time[4096];
float value[4096];
} trace;
void simuTrace(int tmax, float dt, float params[], trace *uneTrace)
{
printf("Shkruani emrin e eksperimentit : \n");
scanf("%s", &uneTrace->comment);
int i = 0;
float v = 0, w = 0, dv = 0, dw = 0, t = 0;
float a = params[0], d = params[1], e = params[2];
while (t <= tmax)
{
dv = (a - v) * (v - 1) * v - w;
dw = e * (0.5 * v - w - d);
v += dv * dt;
w += dw * dt;
uneTrace->time[i] = t;
uneTrace->value[i] = v;
i++;
t += dt;
}
uneTrace->nbpts = i;
}
void printTrace(trace uneTrace)
{
printf("%s\n", uneTrace.comment);
printf("\t%d\n", uneTrace.nbpts);
int i;
for (i = 0; i <= NBPTS; i++) {
printf(" t= %.1f \tv= %.4f \n ", uneTrace.time[i],uneTrace.value[i]);
}
}
void saveTraceBin(char *fileTrace, trace uneTrace)
{
FILE *file;
file = fopen(fileTrace, "w");
if (fopen(fileTrace, "w") == NULL) {
printf("\n Gabim! \n");
} else {
fprintf(file, "%s\n", uneTrace.comment);
fprintf(file, "%d", uneTrace.nbpts);
int i;
for (i = 0; i <= NBPTS; i++) {
fprintf(file, "\n %.1f %.4f",uneTrace.time[i],uneTrace.value[i]);
}
fclose(file);
}
}
void readTrace(char *fileName, trace *uneTrace)
{
FILE*file;
file = fopen(fileName, "r");
if (file != NULL) {
fscanf(file, "%s", uneTrace->comment);
fscanf(file, "%d", &uneTrace->nbpts);
int i;
for (i = 0; i <= NBPTS; i++) {
fscanf(file, "%f", &(uneTrace->time[i]));
fscanf(file, "%f", &(uneTrace->value[i]));
}
printf("\n Leximi perfundoi me sukses!\n");
} else {
printf("\n Gabim! \n");
}
fclose(file);
}
trace* readTrace(char* fileName) {
FILE*file;
file = fopen(fileName, "r");
if (file != NULL) {
fscanf(file, "%s", uneTrace->comment);
fscanf(file, "%d", &uneTrace->nbpts);
int i;
for (i = 0; i <= NBPTS; i++) {
fscanf(file, "%f", &(uneTrace->time[i]));
fscanf(file, "%f", &(uneTrace->value[i]));
}
printf("\n Leximi perfundoi me sukses!\n");
} else {
printf("\n Gabim! \n");
}
fclose(file);
}
float errorTrace(trace uneTrace1, trace uneTrace2)
{
float sum = 0;
int i;
for (i = 0; i <= NBPTS; i++)
{
sum += (uneTrace1.value[i] - uneTrace2.value[i]);
}
sum /= NBPTS;
return sqrt(fabs(sum));
}
int main()
{
float Pa[] = { 0.5, 0.01, 0.05 };
float Pb[] = { 0.75, 0.3, 0.1 };
float e1, e2;
trace tracePa, tracePb, traceCell;
simuTrace(NBPTS, DT, Pa, &tracePa);
printTrace(tracePa);
saveTraceBin("myfile1.txt", tracePa);
simuTrace(NBPTS, DT, Pb, &tracePb);
printTrace(tracePb);
saveTraceBin("myfile2.txt", tracePb);
readTrace("cell.txt", &traceCell);
e1 = errorTrace(traceCell, tracePa);
e2 = errorTrace(traceCell, tracePb);
printf("\n Gabimi nga llogaritja e Pa : %f ", e1);
printf("\n Gabimi nga llogaritja e Pb : %f ", e2);
if (e1 < e2)
printf("\n\n Rasti Pa eshte me i mire se rasti Pb \n");
else
printf("\n\n Rasti Pb eshte me i mire se rasti Pa \n");
return 0;
}
You can either return a trace* that is allocated in readTrace,
trace* readTrace(char* fileName) {
trace *tp = malloc(sizeof *tp);
if (!tp) return NULL;
// fill up tp from file
....
}
// call this in main
trace *t = readTrace("cell.txt");
free(t); // free it when done
Or you could supply trace to the function, like
void readTrace(char* filename, trace *tp) {
if (!tp) return;
// fill up tp from file
....
}
// call this in main
trace t; // define trace object
readTrace("cell.txt", &t);
As for fill up time and value, read the array from the file:
int i;
fscanf(file, "%d", &t->nbpts);
for (i = 0; i < t->nbpts; i++) {
fscanf(file, "%f", &(t->time[i]));
fscanf(file, "%f", &(t->value[i]));
}
The function structure should look like this
trace* readTrace(char* fileName)
{
trace* traceptr;
int no_of_elements;
// Read the no of trace elements stored in file
// Generally this is avaliable in a location in the start of file,
// If not, then you have to guess and resize if it falls short.
traceptr = malloc(sizeof(trace) * no_of_elements);
// Read the trace elements from file
return (traceptr);
}
To call it in main()
int main(void)
{
trace *traceptr;
// Other stuff
traceptr = readTrace(filename);
simuTrace(NBPTS, DT, Pa, traceptr);
}
You need to modify your printTrace function to take a pointer to struct as input instead of an entire structure. Then it can take traceptr as it's input. Similarly for the saveTraceBin function.

Rotate bmp image in C

I'm rotating a bmp image file in C.
I successfully tested copying image(rotate 0 degree). But, It must be a problem in rotating image. Here is my code
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#pragma warning (disable:4996)
#define FILENAMELENGTH 30
#define MAXROW 512
#define MAXCOL 512
//location of pixels
typedef struct
{
int row;
int col;
}COORDI;
//function declarations
void RotatePixel(float Theta, COORDI org, COORDI before, COORDI* after);
void ReadImage(char* original_image);
void WriteImage(char* rotate_image);
char source[MAXROW][MAXCOL];
char dest[MAXROW][MAXCOL];
COORDI org;
float Theta;
int main(void)
{
char original_image[FILENAMELENGTH];
char rotate_image[FILENAMELENGTH];
printf("image name : ");
scanf("%s", original_image);
//rotate angle(radian)
printf("angle : ");
scanf("%f", &Theta);
printf("after rotate file name : ");
scanf("%s", rotate_image);
org.row = MAXROW / 2;
org.col = MAXCOL / 2;
dest[MAXROW][MAXCOL] = { 0, };
ReadImage(original_image);
WriteImage(rotate_image);
printf("rotate completed!\n");
return 0;
}
void ReadImage(char* original_image)
{
FILE* fp;
int i, j;
fp = fopen(original_image, "rb");
if (fp == NULL)
{
printf("File cannot open\n");
exit(-1);
}
for (i = 0; i < MAXROW; i++)
{
for (j = 0; j < MAXCOL; j++)
fread(&source[i][j], sizeof(unsigned char), 1, fp);
}
printf("image read completed !\n");
fclose(fp);
return;
}
//writing a pixel to file
void WriteImage(char* rotate_image)
{
FILE* fp;
int i, j;
COORDI locn;
COORDI* after = (COORDI*)malloc(sizeof(COORDI));
fp = fopen(rotate_image, "wb");
if (fp == NULL)
{
printf("File cannot open\n");
exit(-1);
}
for (i = 0; i < MAXROW; i++)
{
for (j = 0; j < MAXCOL; j++)
{
locn.row = i;
locn.col = j;
RotatePixel(Theta, org, locn, after);
if (after->col < 0) continue;
if (after->row < 0) continue;
if (after->col >= MAXCOL) continue;
if (after->row >= MAXROW) continue;
dest[after->row][after->col] = source[i][j];
putc(dest[after->row][after->col], fp);
}
}
printf("image write completed !\n");
fclose(fp);
return;
}
//rotate pixels
void RotatePixel(float Theta, COORDI org, COORDI before, COORDI* after)
{
int x, y;
x = before.col - org.col;
y = before.row - org.row;
after->col = (int)(x* cos(Theta) - y*sin(Theta)) + org.col; // x
after->row = (int)(x* sin(Theta) + y*cos(Theta)) + org.row; // y
}
I think writing a pixel to file is a problem. where is the missing point?

Variance in data in/out of loop

I'm trying to write a program that reads in a .pdb file, which is a file type used in biology applications. This type of file has a standard format with varying white space between data. The file is of the form
ATOM 4 N ALA 1 2.670 1.801 1.072 0.00 0.00
ATOM 5 CA ALA 1 3.225 3.144 1.197 0.00 0.00
ATOM 6 C ALA 1 4.408 3.341 0.256 0.00 0.00
ATOM 7 O ALA 1 4.553 4.394 -0.363 0.00 0.00
.... . .. ... . ..... ..... ..... ..... ....
So my program (probably poorly written) defines a structure, reads in the data (which I stole from another post here http://www.daniweb.com/software-development/c/threads/65455/reading-a-file-using-fscanf#), and stores it into an indexed struct. Now if I print the values inside of the inner if-loop, it spits out the correct data. However, when I print out the same values outside the outer while-loop, it gives me the wrong atom[].name (which just so happens to be HA, the last value in the 3rd column of data in the input file. All other values are correct.
Here is my program
#include <stdio.h>
typedef struct
{
char *atm;
int serial;
char *name;
char *resName;
int resSeq;
double x;
double y;
double z;
double occupancy;
double tempFactor;
} pdb;
int main(int argc, char** argv)
{
int i, j;
pdb atom[28];
char atm[5];
char name[3];
char resName[4];
int serial;
int resSeq;
double x;
double y;
double z;
double occupancy;
double tempFactor;
char buff[BUFSIZ];
FILE *file = fopen("test.pdb", "r");
i = 0;
while (fgets(buff, sizeof buff, file) != NULL)
{
if (sscanf(buff, "%s %d %s %s %d %lf %lf %lf %lf %lf",
atm, &serial, name, resName, &resSeq, &x, &y, &z,
&occupancy, &tempFactor) == 10)
{
atom[i].atm = atm;
atom[i].serial = serial;
atom[i].name = name;
atom[i].resName = resName;
atom[i].resSeq = resSeq;
atom[i].x = x;
atom[i].y = y;
atom[i].z = z;
atom[i].occupancy = occupancy;
atom[i].tempFactor = tempFactor;
i++;
/*printf("%s ", atom[i].atm);
printf("%d ", atom[i].serial);
printf("%s ", atom[i].name);
printf("%s ", atom[i].resName);
printf("%d ", atom[i].resSeq);
printf("%lf ", atom[i].x);
printf("%lf ", atom[i].y);
printf("%lf ", atom[i].z);
printf("%lf ", atom[i].occupancy);
printf("%lf\n", atom[i].tempFactor);*/
}
}
fclose(file);
for (j = 0; j < i; j++)
printf("%d of %d: %s\n", j, i-1, atom[j].name);
return(0);
}
Any idea why this might be happening? In addition, any help on the program format/structure would also be appreciated. I'm more of a Fortran guy, so C structures are out of my realm of expertise.
Thanks in advance.
EDIT: jsn helped me out and Randy Howard refined it. Here is the updated and working program:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char *atm;
int serial;
char *name;
char *resName;
int resSeq;
double x;
double y;
double z;
double occupancy;
double tempFactor;
} pdb;
int main(int argc, char** argv)
{
int i, j;
pdb atom[28];
int serial;
int resSeq;
double x;
double y;
double z;
double occupancy;
double tempFactor;
char buff[BUFSIZ];
FILE *file = fopen("test.pdb", "r");
i = 0;
while (fgets(buff, sizeof buff, file) != NULL)
{
char *atm = malloc(sizeof(char) * 5);
char *name = malloc(sizeof(char) * 3);
char *resName = malloc(sizeof(char) * 4);
if (sscanf(buff, "%s %d %s %s %d %lf %lf %lf %lf %lf",
atm, &serial, name, resName, &resSeq, &x, &y, &z,
&occupancy, &tempFactor) == 10)
{
atom[i].atm = atm;
atom[i].serial = serial;
atom[i].name = name;
atom[i].resName = resName;
atom[i].resSeq = resSeq;
atom[i].x = x;
atom[i].y = y;
atom[i].z = z;
atom[i].occupancy = occupancy;
atom[i].tempFactor = tempFactor;
i++;
}
}
fclose(file);
for (j = 0; j < i; j++)
{
printf("%s ", atom[j].atm);
printf("%d ", atom[j].serial);
printf("%s ", atom[j].name);
printf("%s ", atom[j].resName);
printf("%d ", atom[j].resSeq);
printf("%lf ", atom[j].x);
printf("%lf ", atom[j].y);
printf("%lf ", atom[j].z);
printf("%lf ", atom[j].occupancy);
printf("%lf\n", atom[j].tempFactor);
}
return(0);
}
Inside the while loop you need to allocate new memory for each char* for each name. You are overwriting them right now.
while (fgets(buff, sizeof buff, file) != NULL)
{
char *atm = malloc(sizeof(char) * 5);
char *name = malloc(sizeof(char) * 3);
char *resName = malloc(sizeof(char) * 4);
if (sscanf(buff, "%s %d %s %s %d %lf %lf %lf %lf %lf",
atm, &serial, name, resName, &resSeq, &x, &y, &z,
&occupancy, &tempFactor) == 10)
You are copying the char array (pointers), so all the names should be the same (the last entry).

function and pointers

I'm trying to write a program that would convert celcius to fahrenheit and visa-versa. My program is compiling, but it gives me wrong results. I'm been trying to change it by changing pointers, but it wouldnt work. Can anyone please point out to me what is my problem? It seems to me it's in declarations and pointers, but i'm not sure.Thanks!
#include<stdio.h>
float f2c(float f);
float c2f(float c);
int main(void)
{
float cel;
float celcius;
float fahren;
float fah;
char ch;
float number;
scanf("%c %f", &ch, &number);
if(&ch == "-f"){
f2c(number);
celcius=f2c(number);
printf("%f", celcius);
}
else{
c2f(number);
fahren = c2f(number);
printf("%f", fahren);
}
return 0;
}
float f2c(float f)
{
float cel = (f - 32) * 5/9;
return cel;
}
float c2f(float c)
{
float fah = (9 * c/5 + 32);
return fah;
}
if(&ch == "-f") this is the problem.
It should be :
if(ch == 'f')
In addition to the other answers:
You are doing needless calls to the conversion functions and throwing away the results, just before doing the same calls again but with proper handling of the return value:
if(&ch == "-f"){
f2c(number); /* This line does absolutely nothing! */
celcius=f2c(number);
printf("%f", celcius);
}
You are comparing char with a C-string "-f". Use strcmp to compare C-strings.
You may also have declare ch as char array to more than one character which you need here. Because, you can't store "-f" in a single char as you do now.
With the changes:
#include<stdio.h>
float f2c(float f);
float c2f(float c);
int main(void)
{
float cel;
float celcius;
float fahren;
float fah;
char ch[25];
float number;
scanf("%s %f", ch, &number);
if(strcmp(ch,"-f")==0){
f2c(number);
celcius=f2c(number);
printf("%f", celcius);
}
else{
c2f(number);
fahren = c2f(number);
printf("%f", fahren);
}
return 0;
}
float f2c(float f)
{
float cel = (f - 32) * 5/9.0;
return cel;
}
float c2f(float c)
{
float fah = (9 * c/5.0 + 32);
return fah;
}
This is essentially your code, but modified it to read the input as C-string.
Use floating-point arithmetic, not integer arithmetic, because in integer arithmetic, 5/9 is 0. So you could make this change to your code (though float parameters are unusual):
float f2c(float f)
{
return (f - 32.0) * 5.0/9.0;
}
float c2f(float c)
{
return 9.0 * c/5.0 + 32.0;
}
Also as one of the other answers states, the condition (&ch == "-f") is never true.
Here is a complete working example, with error checking.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <errno.h>
#include <assert.h>
static double f2c(double f)
{
return (f - 32.0) * 5.0/9.0;
}
static double c2f(double c)
{
return 9.0 * c/5.0 + 32.0;
}
int main (int argc, char *argv[])
{
int i;
const char *label_f = "Fahrenheit";
const char *label_c = "Celcius";
const char *from, *to;
double (*conversion)(double);
from = label_c;
to = label_f;
conversion = c2f;
for (i=1; i<argc; ++i)
{
if (0 == strcmp(argv[i], "-f"))
{
from = label_f;
to = label_c;
conversion = f2c;
}
else if (0 == strcmp(argv[i], "-c"))
{
from = label_c;
to = label_f;
conversion = c2f;
}
else
{
char *suffix = NULL;
double temp_in;
errno = 0;
temp_in = strtod(argv[i], &suffix);
if (ERANGE == errno)
{
if (HUGE_VAL == temp_in)
{
fprintf(stderr, "%s is too big", argv[i]);
return 1;
}
else
{
assert(-HUGE_VAL == temp_in);
fprintf(stderr, "%s is too small", argv[i]);
return 1;
}
}
else if (errno != 0 && (suffix == argv[i]))
{
/* no conversion was performed. */
fprintf(stderr, "%s is not a number", argv[i]);
return 1;
}
else if (*suffix)
{
fprintf(stderr, "Expected a number but saw '%s'\n", argv[i]);
return 1;
}
else
{
const double temp_out = (*conversion)(temp_in);
printf("%f %s is %f %s.\n", temp_in, from, temp_out, to);
}
}
}
return 0;
}

Invalid pointer with pthread_join

#include <pthread.h> /* Thread related functions*/
#include <stdio.h> /* Standard buffered input/output */
#include <stdlib.h> /* Standard library functions */
#include <string.h> /* String operations */
struct element{
FILE* file1;
FILE* file2;
int alone;
};
int comp( const void* a, const void* b) //function for qsort
{
const int *a1 = (const int *)a; // casting pointer types
const int *b1 = (const int *)b;
return *a1 - *b1;
}
void* merge(void* filenames) //writes out files to two arrays, merges them and writes output to temp file and passes this tempfile through pthread_exit.
{
struct element* Data;
Data = (struct element*) filenames;
//char* fileA = Data->file1;
//char* fileB = Data->file2;
FILE* A = Data->file1;
FILE* B = Data->file2;
if(Data->alone!=1)
{
//A = fopen(fileA, "r");
int linesA = 0;
int val=0;
printf("FILE* A: %p \n", A);
rewind(A);
while(fscanf(A, "%d", &val) != EOF)
linesA++;
printf("scanf\n");
rewind(A);
int* intarrA = (int*) malloc(linesA*sizeof(int));
int i =0;
for(;fscanf(A, "%d", &val) != EOF; i++)
intarrA[i] = val;
fclose(A);
//FILE* B;
//B = fopen(fileB, "r");
int linesB = 0;
while(fscanf(B, "%d", &val) != EOF)
linesB++;
rewind(B);
int* intarrB = (int*) malloc(linesB*sizeof(int));
i =0;
for(;fscanf(B, "%d", &val) != EOF; i++)
intarrB[i] = val;
fclose(B);
int templength = linesA+linesB;
int* inttemp = (int*) malloc(templength*sizeof(int));
int help1 = 0;
int help2 = 0;
int temph = 0;
for(i=0; i<templength; i++)
{
if(help1 == linesA)
{
int j = 0;
for(j=help2; j<linesB; j++)
{
inttemp[temph] = intarrB[j];
temph++;
}
break;
}
else if(help2 == linesB)
{
int j = 0;
for(j=help1; j<linesA; j++)
{
inttemp[temph] = intarrA[j];
temph++;
}
break;
}
else if(intarrA[help1]==intarrB[help2])
{
inttemp[temph]=intarrA[help1];
help1++;
help2++;
temph++;
}
else if (intarrA[help1]<intarrB[help2])
{
inttemp[temph]=intarrA[help1];
help1++;
temph++;
}
else if(intarrA[help1]>intarrB[help2])
{
inttemp[temph]=intarrB[help2];
help2++;
temph++;
}
}
FILE* filetowrite;
filetowrite = tmpfile();
for(i=0; i<temph; i++)
fprintf(filetowrite ,"%d\n",inttemp[i]);
printf("Merged %d lines and %d lines into %d lines\n",linesA,linesB,temph);
// free(intarrA);
// free(intarrB);
// free(inttemp);
printf("thread exit\n");
pthread_exit((void*)filetowrite);
}
else{
pthread_exit((void*)Data->file1);
printf("ELSE MERGE \n");
}
}
void* worker(void* filen)
{
//left out to keep code short
}
int main(int argc, char **argv) //gets files through terminal, and sorts each one trhough worker function and then merges them with merge function and pthreads.
{
int zit =0;
void* tempf;
// tempf = malloc(sizeof(FILE));
argc = argc-1;
struct element* filenamelist; //I make an array of this to merge threads till there is only one element left
pthread_t *threadid;
threadid = (pthread_t*) malloc(sizeof(pthread_t)*argc);
int i;
for(i=1; i<=argc; i++) // part 1 passing the files to be qsorted
{
pthread_create(&threadid[i-1], NULL, worker, (void*) argv[i]);
}
//sorts each file fine. saves it as filename.sorted
for(i=0; i<argc; i++)
{
pthread_join(threadid[i], NULL);
}
printf(" DONE WORKER\n");
int mall=0;
int size = 0;
if(size%2 ==0)
size = argc/2;
else
size = argc/2 +1;
//int Osize = size;
int z=0;
int truth = 0;
// filenamelist = (struct element**) malloc(sizeof(struct element*)*argc);
// for(i=0; i<argc; i++)
filenamelist = (struct element*) malloc(argc*(sizeof(struct element)));
FILE** inputFiles = malloc(sizeof(FILE*) * (argc+argc));
for(i=1; i<=argc; i++) //creates a list of elements with file ptrs
{
filenamelist[(i-1)/2].alone = 0;
if(i==argc && i%2!=0)
{
char* tedious1 = (char*) malloc( (strlen(argv[i]) + 8 ));
//char* tedious1 = (char*) malloc((strlen(argv[i+1]+7))*sizeof(char));
strcpy(tedious1,argv[i]);
filenamelist[(i-1)/2].file1 = fopen(strcat(tedious1,".sorted"),"r");
filenamelist[(i-1)/2].file2 = NULL;
filenamelist[(i-1)/2].alone = 1;
free(tedious1);
}
else
{
char* tedious3 = (char*)malloc( (strlen(argv[i]) + 8 ));
strcpy(tedious3,argv[i]);
// printf("%s\n", tedious3);
if(i%2 ==0)
filenamelist[i/2].file2 = fopen(strcat(tedious3,".sorted"),"r");
else
filenamelist[i/2].file1 = fopen(strcat(tedious3,".sorted"), "r");
free(tedious3);
}
}
/* for(i=0; i<size; i++)
{
printf("THIS IS: %d\n", i);
if(filenamelist[i].alone ==1)
{
printf(" Alone \n");
printf(" %p \n", filenamelist[i].file1);
}
else
{
printf("1 %p \n", filenamelist[i].file1);
printf("2 %p \n", filenamelist[i].file2);
}
}*/
// pthread_t* threadid2;
// threadid2 = (pthread_t*) malloc(sizeof(pthread_t)*(2*argc));
int xyz = 0;
int arab = 0;
int saudi = 0;
while(size>1) //Iterates through list till only one element left
{
i = 0;
pthread_t* threadid2;
threadid2 = (pthread_t*) malloc(sizeof(pthread_t)*argc);
printf("before create: %d, %p \n", i, tempf);
for ( ; i<size;i++ )
{
pthread_create(&threadid2[i], NULL, merge, &filenamelist[i]); //<--- invalid pointer
printf("AFTER create: %d, %p \n", i, threadid2[i]);
}
i = 0;
for ( ; i<size; i++)
{
printf("before join JOIN: %d, %p \n", i, tempf);
pthread_join(threadid2[i], &tempf);
printf("AFTER JOIN: %d, %p \n", i, tempf);
if (i%2 == 0)
{
filenamelist[i/2].file1 = (FILE*)(tempf);
}
else
{
filenamelist[i/2].file2 = (FILE*)(tempf);
}
if(i%2==0 && i ==size-1)
filenamelist[i].alone = 1;
zit=0;
truth = 0;
while(zit<z)
{
if(inputFiles[zit] == (FILE*)(tempf))
truth = 1;
zit++;
}
if(truth != 1)
{
inputFiles[z] = (FILE*)(tempf);
z++;
}
}
if(size==1)
size = 0;
else if (size % 2 == 0)
size = size/2;
else
size = (size/2)+1;
free(threadid2);
}
pthread_t throwthread;
pthread_create(&throwthread, NULL, merge, &filenamelist[0]);
FILE* final;
pthread_join(throwthread, tempf);
int chd = 0;
final = (FILE*) tempf;
// if(0!=feof(tempf))
// rewind(tempf);
//rewind(filenamelist[0]->file1);
int finish = 0;
//printf("file 1:%p",tempf);
while(fscanf(final, "%d", &chd) != EOF)
finish++;
rewind(final);
int* finarr = (int*) malloc(finish*sizeof(int));
int xx =0;
for(;fscanf(final, "%d", &chd) != EOF; xx++)
finarr[xx] = chd;
//fclose(filetosort);
//qsort(intarr, i, sizeof(int),comp);
FILE* filetowrites;
filetowrites = fopen("sorted.txt", "w");
for(xx=0; xx<finish; xx++)
fprintf(filetowrites, "%d\n", finarr[xx]);
fclose(filetowrites);
for(xx=0; xx<z; xx++)
fclose(inputFiles[xx]);
free(inputFiles);
free(filenamelist);
free(finarr);
return 0;
}//end main func
This gives me an invalid pointer error. threadid is a pthread_t* pointing to an array.
I'm passing in a file pointer typecasted into a void* for the pthread_exit((void*)fileptr);
where fileptr is some FILE*.
And would it be okay to access tempf as (FILE*) tempf?
EDIT:I've tried everything that makes sense to me. Don't know why threadid2[i] or tempf would be an invalid pointer in the merge function. This is not something i can debug, Help please!
ERROR OCCURS ON FIRST JOIN

Resources