This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
...
main() {
char **src_ip[10];
char **dest_ip[10];
char **lat[10];
char *ip[20];
while{
//Read file
//fgets();
src_ip[j] = &data[0];
dest_ip[j] = &data[1];
lat[j] = &data[2];
int idx;
int addip;
for(idx=0; idx<20; idx++)
{
addip = 0;
//Check to see if the IP address is already in the array.
if ((strcmp(*(src_ip[j]), ip[idx]) == 0) ||
(strcmp(*(dest_ip[j]), ip[idx]) == 0))
{
addip=1;
break;
}
//If the IP address was already found then addip would equal 1.
if (!addip){
printf("new node: %s",*(ip[idx]));
}
else
printf("Exist");
}
}
j++;
}
I want to compare char string in two 1d array - array src_ip[j] and dest_ip[j] - and insert into one array ip[idx]. For example, when I look for ip[1], it will go to src_ip[1], meaning that it's only referring to one array - ip[idx] - instead of looking separately into src_ip and dest_ip.
There must be a problem in the code - maybe I left something out?
if((strcmp(*(src_ip[j],ip[idx]) == 0) && (strcmp(*(dest_ip[j]),ip[idx]) == 0))
^^
You can't have both *(src_ip[j]) and *(dest_ip[j]) equal to ip[idx]. What you meant to use was the || operator instead of &&. (By the way, there's a missing ) where I marked above)
Also your while(idx) doesn't make sense. Are you controlling the loop with the for above, or the while below? If with the while, then you'll get an infinite loop if the condition is true since you are never changing it inside the loop.
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have problem to compile the following lines:
/*This code compiles with error*/
char HeLev1[6];
HeLev1[]="45.0";
/*but this is OK:*/
char HeLev1[6]="45.0";
You cannot assign values to an array. You need to assign values to array elements one-by-one (or, when dealing with strings, using strcpy())
char HeLev1[6];
strcpy(HeLev1, "45.0");
char HeLev2[6];
HeLev2[0] = '4';
HeLev2[1] = '5';
HeLev2[2] = '.';
HeLev2[3] = '0';
HeLev2[4] = '\0'; /* properly "terminate" the string */
Note that in your code, the OK part, you have an array initialization, not assignment.
Also note that, in both cases above, the 6th element (HeLev1[5] or HeLev2[5]) has an undefined value (garbage).
you can assign whole values to an array only while initialization. like these are correct forms,
char HeLev1[6]="45.0";
int array[3]={1,2,3};
char HeLev1[]="45.0";
int array[]={1,2,3};
but once you have skipped this part. you have to assign element by element. like,
char HeLev2[6];
HeLev2[0] = '4';
HeLev2[1] = '5';
HeLev2[2] = '.';
HeLev2[3] = '0';
HeLev2[4] = '\0'; /* properly "terminate" the string */
or you can use memcpy or strcpy.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have the below code. But, I think 4 is the answer. Am I right?
int a, i;
if (execute)
{
int count = 0;
for (i=0; i<5; i++)
{
if(pcnt[i]) count ++;
}
a = (count > 0)
}
else
{
a= 0;
}
Will a be a=1 or a=4 for the IF case?
the a will be 1 or 0
1: If execute != 0 and count >0
0: otherwise
Note:
count >0 if and only if pcnt[0]!=0 or pcnt[1]!=0 or pcnt[2]!=0 or pcnt[3]!=0 or pcnt[4]!=0
First, this won't compile because of a missing semi-colon. It also might have difficulty if the variables are not declared properly, but for the sake of answering, I will assume that they have been.
Now, look at what a is assigned to. I see a = (count > 0) and a = 0. Well (count > 0) will only ever be a 0 or a 1 (in C boolean expressions will resolve to 1 for true and 0 for false). So basically, you have a = 0 or 1 and a = 0. This simplifies to a being either 0 or 1.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am trying to change this into a for loop but im not sure how to.
while((c = getchar()) != EOF){
tweet[cnt] = c;
++cnt;
}
for(cnt = 0; (c = getchar()) != EOF; cnt++)
{
tweet[cnt] = c;
}
While loops and for loops are not so different. The while loop contains only an comparison, if its true it is executed once more until the comparison is made and it not true any more.
The for statement is the same only contains 3 parts separated by ';'.
the first is the part you want to do one time before entering the ever ending cycle.
The second part is the same as the while loop.
The third part is what you want to do after one cycle is done before getting to the next comparison for entering again.
Lot of compilers accept more than one statement in the first and last part. In this case you could get this:
for(cnt=0; , x=0 ; (c=getchar()) != EOF; CNT++, x++){
tweet[cnt] = c;
}
int TheNumberOfCyclesMade = x;
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have problem with realloc. This is my function which reads words from output and is terminated if EOF is detected.
The function makes memory leaks and the following program throws SIGSEGV or SIGABORT. What's a problem ?
int inx=0;
char **wordList=NULL;
int v;
char tmpArr[100];
do
{
v=scanf("%s",tmpArr);
if(v!=-1)
{
char* word=(char*)malloc(strlen(tmpArr)+1);
strcpy(word,tmpArr);
char**more=(char**)realloc(wordList,sizeof(char*)*(inx+1));
if(more!=NULL) {wordList=more;} else return 1;
wordList[inx++]=word;
printf("%d\n",inx);
}
}
v=scanf("%s",tmpArr);
the above can cause a memory overwrite if input string is larger than 100. you may want to use fgets(tmpArray,sizeof(tmpArray),stdin) instead to limit the input to max buffer size (or use scanf_s).
you should not cast what malloc returns, it returns a void* which doesn't need to be cast, if you cast you could mask an error if you forget to include stdlib.h
char* word = /* (char*) */ malloc(strlen(tmpArr)+1);
growing the array everytime you read a new string is not very effective, instead consider allocating a bunch of string pointers or preferably use another data structure e.g. a list
e.g.
if ( inx == maxindex )
{
char**more=(char**)realloc(wordList,sizeof(char*)*(maxindex + bunch));
if (more != NULL)
{
wordList = more;
maxindex += bunch ;
}
else
{
return 1;
}
}
...
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I want to get a string contained within a string where every time I see (xxx)
I will get only xxx.
I am trying to do it in a recursive manner with a function int find(char* str) for example.
The function will return 1 if there are no '(' or ')' left (all removed), and 0 otherwise.
the string can remain as is, just need to check the stated conditions.
OK input: (xxx)(zzz(yyy)zzz)
BAD input: xx(x(zzz)(y
How can this be done in C?
A simple stack is a good way to solve this. Every time you see a '(' push onto the stack. Every time you see a ')' pop off the stack. If you ever try to pop off an empty stack or have things left on the stack when you are done, then the input was bad.
Edit: you could also do the same thing with a counter. Increment and decrement respectively. If the counter ever goes negative, return false. Otherwise, when you are done return true if you are at 0, or false otherwise. So the counter just represents the size of the 'stack'.
I think this could be the answer, continuing the previous from abelenky:
int find(char *str)
{
int pars = 0, pos = 0;
while(str[pos] != 0) {
if (str[pos] == '(') pars++;
if (str[pos] == ')') pars--;
if (pars < 0) return 0; // closes before opens -> end here
pos++;
}
if (pars != 0) return 0; // not matched result
else return 1; // matched result
}