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;
}
}
...
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 10 years ago.
Please tell me what I am doing wrong
//I'm trying to get the number of the month by sending its name.
#include <stdio.h>
My function
int monthstr2num (char month[]){
if (month == "September")
return 8;
}
int main (){
char month []={"September"};
int num;
num = monthstr2num (month);//func call
displays a wrong output like 37814040
printf ("%d", num);
return 0;
}
Your problem lies in two places.
First is where you are using == to compare a string, that isn't possible in C (It's undefined behavior, it compiles but won't do what you want). You must use a function in the C library called strcmp. It's located in string.h and can be used like so:
if(strcmp(month,"September")==0)
return 8;
Also, when that if statement returns false, you must have another return outside the if statement, such as return 0;
This piece of code has 2 problems:
1) (month == "September") compares pointers and not the actual data
2) when (month == "September") is false, the function returns some garbage, because there is no return statement for this case
if (month == "September")
Is wrong. Use strcmp. I'm a little surprised this compiles (as I'm not perfect in my array/pointer subtleties), but this will wind up comparing memory addresses of theses two entities as pointers.
Don't use == to compare strings. C strings are a char *, and == will compare the pointers.
The C standard library provides functions for comparing C strings, e.g.: strcmp, just #include <string.h>.
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.
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
}
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 getting runtime error sigsegv, i don't understand the problem with this code.
this is a program to match initial substring with with array of string having maximum priority.
#include<stdio.h>
#include<limits.h>
int main() {
int T,i,N;
char si[T][1000];
long vi[T];
scanf("%d",&T);
for(i=0;i<T;++i)
scanf("%s%ld",&si[i],&vi[i]);
scanf("%d",&N);
while(N--) {
char str[1000];
scanf("%s",str);
int j,maxPindex=-1;
long maxPriority=LONG_MIN;
int l=strlen(str);
for(j=0;j<T;++j) {
if(strlen(si[j])>=l && strncmp(str,si[j],l)==0 && vi[j]>maxPriority) {
maxPriority=vi[j];
maxPindex=j;
}
}
//free(str);
if(maxPindex==-1) printf("NO\n");
else printf("%s\n",si[maxPindex]);
}
return (0);
}
It crashes already when it tries to create the variable "si". A C variable inside a function, such as the variable "T", starts with random garbage as its value. For example, it could contain 918128238. Then, when trying to create "si", this would be a very large array, and it doesn't fit.
You need to read a value for "T" before "si" and "vi" are created. That is, move your scanf before the declarions of "si" and "vi".