Is there any chance to store many variables without array? [closed] - c

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 7 years ago.
Improve this question
I wanna get some numbers from keyboard. But how to store that number without array[] ? Have i chance to do that ? I don't know exact how many numbers come from keyboard. If i had permission of array, its simple. But array is not allowed.

In your situation, I'd still go with arrays, but if you insist on using pointers, this code below will help you. Regardless of whether you need arrays or pointers, you still need to define an upper limit on how many elements can be stored in memory.
It is now up to you to modify the code to make it efficient and pretty to your assignment needs.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int numelements=10;
int curelement=0;
int* data=calloc(1,numelements*sizeof(int));
int* p=data;
int* res=data;
while (curelement < numelements){
scanf("%d",p);
if (*p==0){break;} //exit if number entered is zero.
p++;
curelement++;
}
//print results
while(*res != 0){
printf("%d ",*res);
res++;
}
free(data);
return 0;
}

Related

Allocating 10kb of memory and printing addresses and content [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to create a program which should allocate 10kb of memory and prints the memory addresses at groups of 4 bytes and its content. Here is what i want the output to be like:
0XAABBCCEB CDCDCDCD
0XAABBCCD8 FFA0B0C0
0XAABBCCD4 00FF00FF
0XAABBCCD0 00000000
I don't really know how to get to that output. I know that i need to use malloc and i know that i need to use the right operator to print it in hexadecimal form, but i don't know how to print the content and how to allocate exactly 10kb
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define _10KB 10240
int main(){
int i;
uint32_t *arr;
if((arr = malloc(_10KB)) == NULL){
perror("malloc failed");
}
for(i = 0; i < (_10KB/4); i++){
printf("0x%p %.8x\n", &arr[i], arr[i]);
}
free(arr);
return 0;
}

What does the following code segment do? [closed]

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 4 years ago.
Improve this question
What does the following code segment do?
#include <stdio.h>
#include <stdlib.h>
int main(){
int num=0;
while(malloc(1<<10)) ++num;
}
First of all, 1<<10 is an expression that can be calculated at compile time, and it equals 1024. So your code is equivalent to
#include <stdlib.h>
int main()
{
int num=0;
while(malloc(1024)) ++num;
}
So what it does is to allocate chunks of 1024 bytes of memory until it fails to do so. Each time, the value of num is increased by one.
Overflowing the variable num will cause undefined behavior because it is signed. However, since you are not using the variable, it is likely to be optimized away.

How pointer variable works, without initialising it in the code in TurboC++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am new to C. I was just interested in making a program which takes two integer input from the user and add it but the condition is that i have to use only one variable. I came up with this code:
#include <stdio.h>
int main()
{
int *a;
scanf("%d%d",a,(a+1));
printf("\nSum=%d",*a+*(a+1));
return 0;
}
scanf() function takes an valid address as an argument and i am passing the value in a(which is not initialised yet). So, how this code worked in Turbo C++?
You are trying to access an area that is not within the scope of the program. Luckily TCC gave it, but I believe if you go on experimenting, results will be undefined.
You can do something like this to solve your problem of adding using 1 variable.
int main()
{
int a;
scanf("%d",&a); //scan the first number
getchar();
a += getchar()-'0'; // get the second number (restricted to 0-9)
printf("%d",a);
return 0;
}

Reading lines of a float and converting to integers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Write a C program that reads lines containing floating point values of type double one per line from the standard input ( e.g. using scanf ), converts those values to integers and then prints those values as right justified integers in a 20-character wide field one per line on the standard output.
#include <stdio.h>
My biggest problem is I don't know where to start. Any tips and help would be appreciated.
The concept is to TYPECAST the float to an integer.
The loop here is for multiple values if you want.
This is the program. I hope this helps; it runs as you want.
#include <stdio.h>
int main()
{
float n;
int t;
//loop here
scanf("%f", &n);
t = (int)n;
printf("%20d", t);
// end loop here
return 0;
}

Searching number without reminder [closed]

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 9 years ago.
Improve this question
What does the main problem in my algorithm,I need to find the smallest positive number which divided from 1 to 20 with out divider...
#include <stdio.h>
#include <stdbool.h>
int main(int argc,char* argv[])
{
int num,j=2;
int saveNum=20;
bool flag = false;
while(!flag)
{
num = saveNum;
while(num%j==0 && j<=20)
{
num /= j;
j++;
}
if(j>20)
flag = true;
else
{
saveNum++;
j=1;
}
}
printf("Done");
printf("%d",saveNum);
}
Are you missing a printf to see what your intermediate results are? That might help you get an idea as to what is going on internally.
But I don't really understand what you're trying to solve. Do you want the result: 2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20?
Because I think that's what you are currently computing. However, you'll overflow before you get there and iterating to that point will take you a while.
If instead you're trying to find the smallest number that is divisible by every number less than or equal to 20, then you may want to revisit your update of num /= j.

Resources