Home > OS >  Passing NULL pointer to pointer and expect allocating for main
Passing NULL pointer to pointer and expect allocating for main

Time:06-14

I was checking pointer to pointer, passing it without allocating. I want to allocate a[0]="something str" a[1]="some thing str" a[2]="something str" a[3]="something str" in pp function. Can I do this (allocating and filling with strcpy in pp function) and get it returned back to main?

This is my attempt:

#include <stdio.h>
#include <malloc.h>
#include <string.h>

#define MAX_LEN 100
// float 3
void pp(char *arr, char *delimiter, char **a)
{
    int i = 0;
    *a = malloc(sizeof(char) * 10);
}

int main(int argc, void **argv)
{
    char *arr = "1.77 1.65 1.56 5.555 6.1";

    char **f;
    pp(arr, " ", &f[0]);
}

I thought I could just allocate individual char * and then populate as strcpy(*(a x),"something") but it causes segfaults at *a = malloc(sizeof(char) * 10);.

CodePudding user response:

For starters the header <malloc.h> is not a standard C header. Instead use header <stdlib.h>.

Aa for your task

yes that what I will eventually do. Basically array that holds float

then you can use the approach shown in the demonstration program below

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

size_t split( const char *s, const char *delim, double **a )
{
    *a = NULL;

    size_t n = 0;

    for ( const char *p = s;  p  = strspn( p, delim ), *p != '\0'; )
    {
          n;
        p  = strcspn( p, delim );
    }

    if ( n != 0 && ( *a = malloc( n * sizeof( double ) ) ) != NULL )
    {
        char *endptr;
        for ( size_t i = 0; i < n; i   )
        {
            ( *a )[i] = strtod( s, &endptr );
            s = endptr;
        }
    }

    return n;
}

int main( void )
{
    char *s= "1.77 1.65 1.56 5.555 6.1";

    double *a;

    size_t n = split( s, " \t", &a );

    if (  a != NULL )
    {
        for ( size_t i = 0; i < n; i   )
        {
            printf( "%.3f ", a[i] );
        }
        putchar( '\n' );
    }

    free( a );
}

The program output is

1.770 1.650 1.560 5.555 6.100 

Actually the function can be more complicated because you will need to check that conversion to double were suvvessful.

CodePudding user response:

You have multiple bugs in your code.

You do not initialized f but access f[0].

You allocate memory for 10 single char but not for pointers in your function.

Also the overall approach is broken.

You could try like this:

#include <stdio.h>
#include <malloc.h>
#include <string.h>

#define MAX_LEN 100

void pp(char *arr,char *delimiter,char ***a)
{
// TODO: Handle NULL pointers.

    int i=0;
    // TODO: Calculate number of strings using arr and delimiter...
    int num = 10;
    *a=malloc((num 1) * sizeof(**a));
    for (int k = 0; k < num; k  )
    {
        (*a)[k] = malloc( 1  length of string to copy) ;
        strcpy((*a)[k], <string to copy>);
    }
    (*a)[num] = NULL; // indicate end of array.
}

int main(int argc,void **argv)
{
    char *arr="1.77 1.65 1.56 5.555 6.1";
    char **f;
    pp(arr, " ", &f);
    int i = 0;
    while (f[i] != NULL)
    {
      printf("string #%d: %s\n", i, f[i]);
      i  ;
    }
}

You should also think about a way how to report the number of found substrings back to the caller. In the example I added an extra pointer holding NULL to terminate the array.

  • Related