Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Improve this question
Why am I getting Segmentation fault? Could you give me some understadable explanation? Thanks in advance.
#include <stdio.h>
int main()
{
int i,j;
char* ips[1000];
char ip[15] = "192.34.132.52";
char port[4] = "4003";
for (i = 0; i < 10; i++) {
sprintf(ips[i], "%s:%d", ip, port);
}
for (j = 0; j < 10; j++) {
printf("[%d] = %s\n", j, ips[j]);
}
return 0;
}
You didn't allocate the memory for ips[i], sprintf doesn't do it for you. Add a line in the first for loop, before the sprintf:
ips[i] = malloc(sizeof(ip)+sizeof(port)+2);
EDIT: as huseyin tugrul buyukisik noted, port isn't big enough to hold 4 characters and a null terminator. And you should use the %s modifier for it as port is a string as well.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void func(char* str) {
int i = 0;
for (i = 0; i < 8191; i += 1) {
str[i] = ('a');
}
}
int main(void) {
char buff[8192] = { 0, };
int len = 0;
func(buff);// printf("%s\n", buff);
len = strlen(buff);
printf("len:%d\n");
//printf("%s\n",buff);
return 0;
}
I try to expect the len : 8191 ,
but returns wrong number..
why is this happen??
could you explain why this happens??
printf("len:%d\n"); is incorrect. For each conversion specification such as %d, there must be an argument in the function call that gives the value to be printed. It should be printf("len:%d\n", len);.
Your compiler likely warned you of this. If it did not, enable warnings in your compiler and pay attention to them.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to create an array of strings but I keep getting an error.
Can you help me figure out what's wrong with this code?
int size;
scanf("%d",&size);
char** arr;
arr=(char**)malloc(sizeof(char*)*size);
You can simply use array of n number of pointers to char. Then use a loop to allocate space for those.
int n, size;
scanf("%d %d", &n, &size);
char *arr[n];
for( int i = 0; i < n; ++i ){
arr[i] = malloc( size * sizeof(char) );
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to choose the distinct initial seeds to generate different random numbers in [0,1] in a for loop with C?
Vague answer to a vague question :)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int i;
srand(time(NULL));
for(i = 0; i < 100; i++){
printf("%i ", rand()%2);
}
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have an array contain 100 elements. Can anyone help me to figure out how to write loops that perform this:
data[0] = 1/(2*3*4)
data[1] = 1/(4*5*6)
data[2] = 1/(6*7*8)
...
data[99] = 1/(200*201*202)
data[0]-data[1]+data[2]-data[3]+data[4]-data[5]+...+data[98]-data[99]
I just can't understand how to start. Any suggestions would be appreciated!
Try this
double c=0;
for (int i=0;i<100;i++)
{
c=i*2+2;
data[i]=1/(c*(c+1)*(c+2));
}
for (int i = 0; i < 100; i+=2)
{
op+= data[i] - data[i+1];
}
My suggestions how to start, if you really want them and want to manage this by your own:
Generalize your algorithm:
Find a function f(x) such that data[i] = f(i)
Just write the algorithm in your native language.
Then learn basic operators of C language, including loop construct.
Write your "native language algorithm" in C language.
Just in one loop:
int total = 0;
for(size_t i=0; i<100; ++i){
int temp = (i+1)*2;
data[i] = 1/(temp*(temp+1)*(temp+2));
total = total + (i%2==0?data[i]:-data[i]);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I know that a string should be n+1 in length, but for some reason my program is printing the sizeof(string) as n-2.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char name [] = "Tom";
int x = sizeof(name);
int i;
printf("sizeof(name) = %d\n", i);
for(i = 0; i < x; i++)
{
printf("Character at %d is %c\n", i, name[i]);
}
return 0;
}
Can anyone explain why?
You're printing i, not x.
i was never initialized, so you get undefined behavior.