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.
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'm trying to covert an int to char. Is there any way to do that?
For example:
{
int i;
char d;
i = 55;
d = i;
printf("%c\n", d);
}
How do I make d = 55?
If you want to put the number 55 into a string, use sprintf
Indeed your example can do what you want.
If you really want to place safe, you may:
d = (char) i;
Try this code segment:
printf("%d\n", d);
char are presented in the memory as binary format wich is equivalent to a number and this number is called a code ascii. when you print the code ascii with "%c" Then it will print the charchter equivalent to this code ascii
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.
For some reason, my sprintf call is affecting the string that I used to format the new string. Here is my code:
string foo = "bar";
char salt[] = "";
sprintf(salt, "%c%c", foo[0], foo[1]);
When I try printing foo after the sprintf, it has no value. If I print it before the sprintf, it's fine.
Your result buffer(salt) is too small to hold the value.
string foo = "bar";
char salt[3] = "";
sprintf(salt, "%c%c", foo[0], foo[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.
...
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 was asked this on a practice test, that has no answers posted. I have no way to test the code, but it has confused me. Can you please help me out with not only understanding the answer, but why.
int foo() {
int a = 1;
char b[] = "zapples";
a = *(b + 1);
if (a == 'a') return 1;
else return 0;
}
What does foo return? ____
Have they taught you how C pointers work?
I'm not going to give you a straight answer, but think about this:
b has the starting address of string "zapples". This means that b[0] points to "z". Another notation for this is *(b + 0), that is, "the value contained at address b, with an offset of 0). With this information, what is the value of *(b + 1)?
This should be enough to solve the exercise.
It will return 1.
The line a = *(b + 1) is the important one. It takes b as a pointer to the first element in the array and adds one so it points to the second. It is then dereferenced so that the value at that address 'a' is assigned to the variable a.