c

char * return (null) from a function in C [duplicate]

与世无争的帅哥 提交于 2021-02-17 07:10:23
问题 This question already has answers here : Returning an array using C (8 answers) Closed last month . I was trying to append two const char* from a function but it returns (null) . What should I do now? I'm working in VS Code on Ubuntu 20.04 using GCC Compiler v9.3.0. Code #include <string.h> #include <stdio.h> char *JoinChars(const char *a, const char *b) { char buffer[strlen(a) + strlen(b) + 1]; strcpy(buffer, a); strcat(buffer, b); return buffer; } int main() { const char *b1 = "Hello\t";

C program console exits immediately after execution?

柔情痞子 提交于 2021-02-17 07:09:13
问题 I am using Code::Blocks for programming and yeah i am a beginner but everytime i write a program it pauses in IDE but does not pause while executing directly. What is the possible reason ? Can anyone explain me ? My code goes as follows : #include <stdio.h> void main() { float length,breadth,Area; printf("Enter the value of length and breadth \n\n"); scanf("%f %f",&length,&breadth); printf("You entered length=%f and breadth=%f \n\n",length,breadth); Area= length * breadth; printf("The area of

C program console exits immediately after execution?

主宰稳场 提交于 2021-02-17 07:09:09
问题 I am using Code::Blocks for programming and yeah i am a beginner but everytime i write a program it pauses in IDE but does not pause while executing directly. What is the possible reason ? Can anyone explain me ? My code goes as follows : #include <stdio.h> void main() { float length,breadth,Area; printf("Enter the value of length and breadth \n\n"); scanf("%f %f",&length,&breadth); printf("You entered length=%f and breadth=%f \n\n",length,breadth); Area= length * breadth; printf("The area of

Simple <Time.h> program takes large amount CPU

最后都变了- 提交于 2021-02-17 07:08:48
问题 I was trying to familiarize myself with the C time.h library by writing something simple in VS. The following code simply prints the value of x added to itself every two seconds: int main() { time_t start = time(NULL); time_t clock = time(NULL); time_t clockTemp = time(NULL); //temporary clock int x = 1; //program will continue for a minute (60 sec) while (clock <= start + 58) { clockTemp = time(NULL); if (clockTemp >= clock + 2) { //if 2 seconds has passed clock = clockTemp; x = ADD(x);

Looking for help regarding a greedy algorithm in C for the cs50 “cash” problem set [closed]

拟墨画扇 提交于 2021-02-17 07:08:08
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 months ago . Improve this question The goal is to make an algorithm that takes an input and gives an output of the number of times the values ( 25, 10, 5, 1) are subtracted from the input. The code needs to do this in the greediest way possible, taking the highest value whenever its possible. Expected output

Simple <Time.h> program takes large amount CPU

一笑奈何 提交于 2021-02-17 07:08:06
问题 I was trying to familiarize myself with the C time.h library by writing something simple in VS. The following code simply prints the value of x added to itself every two seconds: int main() { time_t start = time(NULL); time_t clock = time(NULL); time_t clockTemp = time(NULL); //temporary clock int x = 1; //program will continue for a minute (60 sec) while (clock <= start + 58) { clockTemp = time(NULL); if (clockTemp >= clock + 2) { //if 2 seconds has passed clock = clockTemp; x = ADD(x);

Fill 2d array in spiral order in c

淺唱寂寞╮ 提交于 2021-02-17 07:07:30
问题 I'm doing program where I enter the number from keyboard. Then 2d array is created, and it's filled in spiral order till this number. All elements after the number will be equal to 0; The function fills the array in spiral order (to right -> down -> left -> up). Code is: #include <stdio.h> #include <time.h> void spiral(int array[100][100], int m, int n, int s) { int size, b, x = 0, y = 1, num = 1; size = m*n; for (num=1;num<=size+1;num++) { for (b = x; b < n; b++) { if (num <=s) { array[x][b]

Initialize 2D array dynamically only knowing its column size in C [closed]

耗尽温柔 提交于 2021-02-17 07:05:20
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 months ago . Improve this question I need to create a 2D Array with 10 columns and an unspecified row amount. My idea is to malloc the array but unfortunately I don't know how to do that with a 2D Array. Can someone provide me with a simple code? So far, I have this: int arr[][10]; int rows = malloc(sizeof(int

Initialize 2D array dynamically only knowing its column size in C [closed]

不打扰是莪最后的温柔 提交于 2021-02-17 07:04:59
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 months ago . Improve this question I need to create a 2D Array with 10 columns and an unspecified row amount. My idea is to malloc the array but unfortunately I don't know how to do that with a 2D Array. Can someone provide me with a simple code? So far, I have this: int arr[][10]; int rows = malloc(sizeof(int

ANSI C - Text File: Modify Line?

♀尐吖头ヾ 提交于 2021-02-17 06:50:10
问题 I have this text file: Line 1. "house" Line 2. "dog" Line 3. "mouse" Line 4. "car" ... I want to change Line 2. "dog" in new Line 2."cards" how can I do? thanks! (sorry for my bad English) 回答1: Your program could like this: #include <stdio.h> #include <stdlib.h> #define MAX_LINE_LENGTH 1000 int main() { FILE * fp_src, *fp_dest; char line[MAX_LINE_LENGTH]; fp_src = fopen("PATH_TO_FILE\\test.txt", "r"); // This is the file to change if (fp_src == NULL) exit(EXIT_FAILURE); fp_dest = fopen("PATH