Also I again missed the ==! But despite that the code fails!
Printable View
Also I again missed the ==! But despite that the code fails!
I got the problem but don't get how to fix it!
Since I use:
when I change anything in i_data the same change is copied in data too!Can do something so that only values are copied?Code:
for (i=0;i<MAX_IP_DATA_ROWS;i++)
{
i_data[i] = (char *)malloc(sizeof(data[i]));
i_data[i]=data[i];
}
Thanks,
Angkar
sigh....did you ignore my comment that you shouldn't typecast malloc?
do val = malloc(sizeof(data[end]) * sizeof(char )); instead..
char * rev_sign(char *ip)
? your function returns a pointer? no....
char rev_sign(char *ip)
you're not returning anything. you're passing the address of your array to ip. so when you do something to ip, you're actually doing it to your original string. the changes are kind of "passed on".
why are you +ing start and -ing end?
why dont you post your COMPLETE code..? that would make help much easier to give.
one last thing , there's an "edit" button on the posts for a reason :P USE IT instead of making more and more posts, lol its a waste of space.
Here it is:
Again, I'm too weak in pointers. Please help me rectify my errors!Code:
void invert(char *data[],int p1,int p2)
{
//printf("\n Inversion");
int i,j=0;
int diff = abs(p2-p1);
char *val;
char *i_data[MAX_IP_DATA_ROWS];
for (i=0;i<MAX_IP_DATA_ROWS;i++)
{
i_data[i] = (char *)malloc(sizeof(data[i]));
i_data[i]=data[i];
}
//printf("Hi%s\n",data[0]);
if (diff==1)
{
val = rev_sign(i_data[p1]);
strcpy(i_data[p1],val);
}
else
{
int start;
int end;
for (start = p1,end = p2-1;start<=end;start++,end--)
{
val = rev_sign(data[start]);
strcpy(i_data[end],val);
val = rev_sign(data[end]);
strcpy(i_data[start],val);
}
}
while(j<MAX_IP_DATA_ROWS)
{
puts(i_data[j]);
j++;
}
}
char * rev_sign(char *ip)
{
if(ip[0]=='+')
ip[0]='-';
else
ip[0]='+';
return ip;
}
where is your main function??? is that your COMPLETE source?? how are you calling your invert function? or getting data to pass to your invert function? or anything? lol when i said to post your complete code, i wasn't like..pulling your leg or meaning your complete code for the functions. i figured complete meant all of it, lol
If you are so weak in pointers, why do you bother trying to make an array of strings? Why must you have an array of strings anyway?
Again my advice is to simplify your life. Start the program over and just use strings. Once the program works the way you want, you now have a firm grasp of the logic.
Then if you simply have to you can get it to work with arrays of strings if you really want to. At least with this design method in mind you can't totally ruin the program's logic while your doing something.