#is this wrong or just outdated

19 messages · Page 1 of 1 (latest)

distant acorn
#
#include <stdlib.h> 
#include <stdio.h> 
#include <time.h> 
 
int main() { 
 // Allows you to generate random number 
 srand(time(NULL)); 
 
 // Allows user to specify the original array size, stored in variable n1. 
 printf("Enter original array size: "); 
 int n1 = 0; 
 scanf("%d", &n1); 
 
 // Create a new array of n1 ints 
 int *a1 = malloc(n1 * sizeof(int)); 
 
 for(int i = 0; i < n1; i++) { 
  // Set each value in a1 to 100 
  *(a1+i) = 100; 
   
  // Print each element out (to make sure things look right) 
  printf("%d ", *(a1+i)); 
 } 
 
 // User specifies the new array size, stored in variable n2. 
 printf("\nEnter new array size: "); 
 int n2 = 0; 
 scanf("%d", &n2); 
 
 // Dynamically change the array to size n2 
 a1 = realloc(a1, n2* sizeof(int)); // Resize the array 
 
 // If the new array is a larger size, set all new members to 0.  
 // Reason: dont want to use uninitialized variables. 
 
 if (n2 > n1) { 
  for(int j = n1; j<n2; j++) { 
   *(a1+j)=0; 
  } 
 } 
 
 for (int i = 0; i < n2; i++) { 
  //Print each element out (to make sure things look right) 
  printf("%d ", *(a1+i)); 
 } 
 printf("\n"); 
  
 // Done with array now, done with program :D 
 free(a1); 
 
 return 0; 
}
coarse egretBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

distant acorn
#
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main(){
    //Allows you to generate random number
    srand(time(NULL));

    // Allows user to specify the original array size, stored in variable n1.
    printf("Enter original array size:");
    int n1=0;
    scanf(" %d",&n1);

    //Create a new array of n1 ints
    int* a1 = (int*)malloc(n1);
    int i;
    for(i=0; i<n1; i++){
        //Set each value in a1 to 100
        a1[i]=100;
        
        //Print each element out (to make sure things look right)
        printf("%d ", a1[i] );
    }

    //User specifies the new array size, stored in variable n2.
    printf("\nEnter new array size: ");
    int n2=0;
    scanf(" %d",&n2);

    //Dynamically change the array to size n2
    a1 = (int*)realloc(a1, n2);

    //If the new array is a larger size, set all new members to 0. Reason: dont want to use uninitialized variables.

    if(n2 > n1) for(i=n1; i<n2;i++) a1[i] = 0;

    for(i=0; i<n2;i++){
        //Print each element out (to make sure things look right)
        printf("%d ",a1[i]);
    }
    printf("\n");
    
    //Done with array now, done with program :D
    free(a1);
    return 0;
}

my version but still doesn't run

random axle
#

wdym by 'outdated'?
its valid code (the original snippet, yours has some flaws in it). aside from the redundant call to srand and the use of int for indices - i don't see why it wouldn't run

distant acorn
random axle
#

fixed mine
i don't think so. your malloc & realloc calls aren't doing what you think they do (the cast is unnecessary too)

random axle
distant acorn
#

same thing, no ?

random axle
#

no. g++ is the compiler for cpp, gcc is the compiler for C. C != cpp

distant acorn
#

im using gcc

random axle
#

in c a void * may be implicitly converted to any other ptr type (beside function pointers), cpp doesn't allow that which leads me to believe whatever you're using to compile that code is likely a cpp compiler

distant acorn
random axle
#

vsc isn't a compiler. check your config files and see whats vsc is calling

random axle
#

figured.
please fix your allocations, and while you're at it - make sure to compile with warnings enabled. -Wall -Wextra at the very least

coarse egretBOT
#
How to Enable Compiler Warnings and Analyzers

Enabling compiler warnings is extremely important to ensure that your code is correct. Countless mistakes can be caught by the compiler before you ever run your code, such as missing return statements, unused variables, etc.

Static analyzers run additional tests on your code, which are much more expensive. They can spot more complex mistakes, such as dereferencing null pointers.

Warnings for GCC and clang
Warnings for MSVC
  • recommended: /W4
  • warnings as errors: /WX
  • disble warnings: /wd
  • analysis: /analyze
Passing Warning Flags to the Compiler on the Command Line
g++ -Wall -Werror -o program main.cpp
Passing Warning Flags indirectly using CMake
# GCC/clang-specific, needs compiler detection
target_compile_options(program PRIVATE -Wall -Werror)