run-time errors - pointers in c [closed] - c

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 wrote a program which prints all the letters in English but there is a run - time error in the code which I wrote. Where is it? Why did it happened?
Can you please help me and solve it?
void printArray(char* p, int len)
{
for( p ; p < p + len ; p++ )
{
printf("%c", *p);
}
printf("\n");
}
int main()
{
char* abc = "abcdefghijklmnopqrstuvwxyz";
printArray(abc, 26);
return 0;
}

When will p not be less that p + len?
for( p ; p < p + len ; p++ ) // Loop forever
You might want something like:
char* stop = p + len;
for( p ; p < stop ; p++ )

p < p + len is never false --> infinite loop.
Suggest
for(int i = 0; i<len ; i++ )
{
printf("%c", p[i]);
}

Because you are editing p as you check against it "p < p + len" this loop will never end. p will always be less than itself plus a constant.
try it like
char* end = p + len;
for(p ; p < end; p++)
then it should work

Related

why could my code be stopping at this point in my code? [closed]

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 3 years ago.
Improve this question
My code runs until it reaches this section of code then it stops
int i,y;
short int** audiodata = (short int **)calloc(nsample*2, sizeof(short int*));
for( i=0 ; i<nsample ; i++)
{
for( y=0 ; y<1 ; y++)
{
audiodata[i][y]= (short int) (32700.0* sin(2*pi*freq*(float)i/44100) );
}
}
what errors am i not seeing and i have a printf statement after this section of code that let the user know it finished loading the 2-d array but the printf never get executed.
The problem is that you did not allocate arrays of pointers of the type short int *. So this statement
audiodata[i][y]= (short int) (32700.0* sin(2*pi*freq*(float)i/44100) );
^^^^^^
has undefined bahavior.
What you mean is the following
short int** audiodata = calloc( nsample, sizeof( short int* ) );
for ( i = 0; i < nsample; i++ )
{
audiodata[i] = calloc( 2, sizeof( short int ) );
}
for ( i = 0; i < nsample; i++ )
{
for ( y = 0; y < 2; y++ )
{
audiodata[i][y]= (short int) (32700.0* sin(2*pi*freq*(float)i/44100) );
}
}

C: array sum using pointers [closed]

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 4 years ago.
Improve this question
How could I get the sum of an array using only pointers ?
int array_sum(int *begin, int *end)
{
int arraysum = 0;
int *p = 0;
for (p = begin; p < end; p ++)
{
arraysum += *(arr + p);
}
return arraysum;
}
this doesn't compile at line
arraysum += *(arr + p);
You could do something like this.
int array_sum(int *begin, int *end)
{
int arraysum = 0;
while (begin < end)
{
arraysum += *begin;
++begin;
}
return arraysum;
}
for (p = begin; p < end; p ++)
{
arraysum += *(arr + p);
}
This doesn't work because you can't add arr and p. You don't need to add anything to p, your number is in *p.
As to why: When you iterate over indicies, i.e. 0 to arraylength - 1, you always use the begin pointer and add the index to it to find you number. In your loop however, you copy the beginning pointer to p and increment P directly, so p starts at the first number, once incremented points at the second number, and so on, until p and end are the same, note the p < end as condition.
dont understand how the loop is incremented. we get at first p = begin
(adress number) right ? What does it exactly do at p ++ ? It does p +
1 right ? but adresses of int are of size 4 ???
Pointer-Arithmetic makes this possible. Math on pointers works a bit different, essentially making sure that you add/subtract multiples of the objects size. So adding one to your int-pointer actually adds sizeof(int) under the hood. This is also how array-indicies work, array[i] is equivalent to *(array + i).

What is causing a segmentation fault in this function? [closed]

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 5 years ago.
Improve this question
So, as an exercise, I am developing a code that uses a recursive function in order to make a simple mathematical expressions calculator. The problem is, I am getting a segmentation fault (6) or (11) when I run it, but I have checked a hundred times and each function call seems to be accessing memory only from the variables in the function just above it on the stack, provided by the pointer *init. Where am I getting this wrong?
Code is as follows:
int solve(char *expression, int *init) {
int result;
int l = strlen(expression);
int i = *init;
//Inicializing result:
for (int n = 0; n <= l; n++) {
if ((expression[n]=='1')||(expression[n]=='2')||(expression[n]=='3')||(expression[n]=='4')||(expression[n]=='5')||
(expression[n]=='6')||(expression[n]=='7')||(expression[n]=='8')||(expression[n]=='9')) {
result = expression[n]-48;
break;
}
}
//Doing calculations:
int j = i;
for (j; j <= l; j++) {
if (expression[j] == '(') {
result = result + solve(expression, &j);
}
if (expression[j] == '+')
result = result + (expression[j+1]-48);
if (expression[j] == '-')
result = result - (expression[j+1]-48);
if (expression[j] == '*')
result = result * (expression[j+1]-48);
if (expression[j] == '/')
result = result / (expression[j+1]-48);
if (expression[j] == ')')
return result;
}
return result;
}
There is infinite recursion in the code
int solve(char *expression, int *init) {
/* ... */
int j = i;
for (j; j <= l; j++) {
if (expression[j] == '(') {
result = result + solve(expression, &j);
/* ... */
If it ever founds a '(' in expression, then the function is called over and over again with the same parameters, until the program stack is filled up. Then you get a segfault.
When using loops or recursion, you should always make sure that the construct eventually terminates, i.e. it gets demonstrably closer to the terminating condition on every iteration. It's trivial to see when doing a simple loop like for(i=0;i<x;i++), but considerably harder when the iteration and the terminating conditions are scattered over a recursive function.
It's not the only problem with your code, but arguably the most severe one.

Can't find the bug in my insertion sort program [closed]

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 7 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, key, p;
int A[] = {1, 3, 2, 4, 9, 8, 7};
for (j=1; j<7; j++){
key = A[j];
//insert A[j] into the sorted sequence
i = j-1;
while(i>0 && A[i]>key){
A[i+1] = A[i];
--i;
}
A[i+i] = key;
}
for(i=0; i<7; ++i){
printf("%d ", A[i]);
}
return 0;
}
The above stated code is supposed to perform an insertion sort, however, I am getting undesirable sort with few repeated values. I can't seem to figure out where I am going wrong.
You have a typo.
A[i+i] = key;
should be
A[i+1] = key;
Also, you have an unused variable p
There are two mistakes in your Insertion Sort. The first one has pointed out by Arun A. S.
A[i+1] = key; instead of A[i+i] = key;
while(i>=0 instead of while(i>0
You need a slight modification to your code( the typo A[i+i] = key; was not the only problem )
i = j ;
while(i>0 && A[i-1]>key){
A[i] = A[i-1];
--i;
}
A[i] = key;
I kept the condition while(i>0 and changed the offsets, but you could keep them and change only this line while(i>=0 instead.

statement with no effect for-loop C [closed]

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 8 years ago.
Improve this question
I got this for-loop:
for(int k=4;k<0;k--){
if(k == 0){
test[k] = 5;
break;
}
else{
test[k] = test[k-1];
}
}
it should shift the elements of the array to the right, but nothing happens.
To my knowledge it should work just fine, but the compiler says, that the for-loop statement has no effect: statement with no effect [-Wunused-value]
can anyone help me solve this problem?
The loop initializes k to a positive value (k=4) and then loops while k is negative (k<0).
Since k is never negative, the loop has no effect.
Did you mean to write k >= 0?
The condition expression of the loop is wrong.
for(int k=4;k<0;k--){
You initialized k with 4 and then are checking whether it is less than 0. As 4 is obviously greater than 0 then the loop will iterate never.
I think you mean the following
for(int k = 4; k >= 0; k-- ) {
But in any case the code looks badly. For example it is not clear what the magic number 5 means and there is no need to use the break stztement.
You could write a function. Here is an example of the corresponding program
#include <stdio.h>
void shift_right( int a[], size_t n )
{
if ( n > 1 )
{
size_t i = n - 1;
int last = a[i];
for ( ; i != 0 ; --i ) a[i] = a[i - 1];
a[i] = last;
}
}
int main(void)
{
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const size_t N = sizeof( a ) / sizeof( *a );
size_t i;
for ( i = 0; i < N; i++ ) printf( "%d ", a[i] );
puts( "" );
shift_right( a, N );
for ( i = 0; i < N; i++ ) printf( "%d ", a[i] );
puts( "" );
return 0;
}
The output is
0 1 2 3 4 5 6 7 8 9
9 0 1 2 3 4 5 6 7 8
In your loop you use condition k<0 -> 4<0 -> false.
so your condition fails in first time itself.so your loop has not executed.so change your conditional statement to k>0 instead of k<0.
use the printf statement to find the loop is executes or not.
for(int k=4;k>=0;k--){
if(k == 0){
test[k] = 5;
break;
}
else{
test[k] = test[k-1];
}
}
The above code works as you expected.

Resources